Package org.assertj.core.api
Class AbstractThrowableAssert<SELF extends AbstractThrowableAssert<SELF,ACTUAL>,ACTUAL extends Throwable>
java.lang.Object
org.assertj.core.api.AbstractAssert<SELF,ACTUAL>
org.assertj.core.api.AbstractObjectAssert<SELF,ACTUAL>
org.assertj.core.api.AbstractThrowableAssert<SELF,ACTUAL>
- Type Parameters:
SELF
- the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.ACTUAL
- the type of the "actual" value.
- All Implemented Interfaces:
Assert<SELF,
,ACTUAL> Descriptable<SELF>
,ExtensionPoints<SELF,
ACTUAL>
- Direct Known Subclasses:
ThrowableAssert
public abstract class AbstractThrowableAssert<SELF extends AbstractThrowableAssert<SELF,ACTUAL>,ACTUAL extends Throwable>
extends AbstractObjectAssert<SELF,ACTUAL>
Base class for all implementations of assertions for
Throwable
s.- Author:
- David DIDIER, Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Jack Gough, Mike Gilchrist
-
Field Summary
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself, objects, throwUnsupportedExceptionOnEquals
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractThrowableAssert
(ACTUAL actual, Class<?> selfType) -
Method Summary
Modifier and TypeMethodDescriptioncause()
Returns a new assertion object that uses the cause of the current Throwable as the actual Throwable under test.void
Verifies that theThrowableAssert.ThrowingCallable
didn't raise a throwable.getCause()
Deprecated.Deprecated.userootCause()
instead.protected SELF
Verifies that the actualThrowable
has a cause similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).hasCauseExactlyInstanceOf
(Class<? extends Throwable> type) Verifies that the cause of the actualThrowable
is exactly an instance of the given type.hasCauseInstanceOf
(Class<? extends Throwable> type) Verifies that the cause of the actualThrowable
is an instance of the given type.hasCauseReference
(Throwable expected) Verifies that the actualThrowable
has a cause that refers to the given one, i.e.hasMessage
(String message) Verifies that the message of the actualThrowable
is equal to the given one.hasMessage
(String message, Object... parameters) Verifies that the message of the actual (@code Throwable) is equal to the given one, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.hasMessageContaining
(String description) Verifies that the message of the actualThrowable
contains the given description.hasMessageContaining
(String description, Object... parameters) Verifies that the message of the actualThrowable
contains the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.hasMessageContainingAll
(CharSequence... values) Verifies that the message of the actualThrowable
contains all the given values.hasMessageEndingWith
(String description) Verifies that the message of the actualThrowable
ends with the given description.hasMessageEndingWith
(String description, Object... parameters) Verifies that the message of the actualThrowable
ends with the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.hasMessageFindingMatch
(String regex) Verifies that a sequence of the message of the actualThrowable
matches with the given regular expression (seeMatcher.find()
).
ThePattern
used under the hood enables thePattern.DOTALL
mode.hasMessageMatching
(String regex) Verifies that the message of theThrowable
under test matches the given regular expression.hasMessageMatching
(Pattern regex) Verifies that the message of theThrowable
under test matches the given regular expressionPattern
.hasMessageNotContaining
(String content) Verifies that the message of the actualThrowable
does not contain the given content or isnull
.hasMessageNotContainingAny
(CharSequence... values) Verifies that the message of the actualThrowable
does not contain any of the given values or isnull
.hasMessageStartingWith
(String description) Verifies that the message of the actualThrowable
starts with the given description.hasMessageStartingWith
(String description, Object... parameters) Verifies that the message of the actualThrowable
starts with the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Verifies that the actualThrowable
does not have a cause.Verifies that the actualThrowable
has no suppressed exceptions.hasRootCause
(Throwable cause) Verifies that the actualThrowable
has a root cause similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).hasRootCauseExactlyInstanceOf
(Class<? extends Throwable> type) Verifies that the root cause of the actualThrowable
is exactly an instance of the given type.hasRootCauseInstanceOf
(Class<? extends Throwable> type) Verifies that the root cause of the actualThrowable
is an instance of the given type.hasRootCauseMessage
(String message) Verifies that the message of the root cause of the actualThrowable
is equal to the given one.hasRootCauseMessage
(String message, Object... parameters) Verifies that the message of the root cause of the actualThrowable
is equal to the given one, after being formatted usingString.format(String, Object...)
method.hasStackTraceContaining
(String description) Verifies that the stack trace of the actualThrowable
contains the given description.hasStackTraceContaining
(String description, Object... parameters) Verifies that the stack trace of the actualThrowable
contains the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.hasSuppressedException
(Throwable suppressedException) Verifies that the actualThrowable
has a suppressed exception similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).message()
A shortcut forextracting(Throwable::getMessage, as(InstanceOfAssertFactories.STRING))
which allows to extract a throwable's message and then execute assertions on it.Returns a new assertion object that uses the root cause of the current Throwable as the actual Throwable under test.Methods inherited from class org.assertj.core.api.AbstractObjectAssert
as, as, doesNotReturn, extracting, extracting, extracting, extracting, extracting, extracting, extractingForProxy, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, hasOnlyFields, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveAssertion, usingRecursiveAssertion, usingRecursiveComparison, usingRecursiveComparison
Methods inherited from class org.assertj.core.api.AbstractAssert
areEqual, asInstanceOf, asList, assertionError, asString, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, hasToString, inBinary, inHexadecimal, is, isElementOfCustomAssert, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, overridingErrorMessage, satisfies, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, satisfiesAnyOfForProxy, satisfiesForProxy, setCustomRepresentation, setDescriptionConsumer, setPrintAssertionsDescription, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withFailMessage, withRepresentation, withThreadDumpOnError
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.assertj.core.api.Descriptable
as, describedAs, describedAs
-
Constructor Details
-
AbstractThrowableAssert
-
-
Method Details
-
hasBeenThrown
-
hasMessage
Verifies that the message of the actualThrowable
is equal to the given one.- Parameters:
message
- the expected message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
is not equal to the given one.
-
hasMessage
Verifies that the message of the actual (@code Throwable) is equal to the given one, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Example:
Throwable invalidArgException = new IllegalArgumentException("foo is not a valid input"); Throwable throwable = new Throwable(invalidArgException); // This assertion succeeds: assertThat(throwable).hasMessage("%s is not a valid input", "foo"); // These assertions fail: assertThat(throwable).hasMessage("%s is not a valid input", "bar"); assertThat(throwable).hasMessage("%s is not a valid input", 12); assertThat(null).hasMessage("%s is not a valid input", "foo");
- Parameters:
message
- a format string representing the expected messageparameters
- argument referenced by the format specifiers in the format string- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
is not equal to the given one.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasCause
Verifies that the actualThrowable
has a cause similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).Example:
Throwable invalidArgException = new IllegalArgumentException("invalid arg"); Throwable throwable = new Throwable(invalidArgException); // This assertion succeeds: assertThat(throwable).hasCause(invalidArgException); // These assertions fail: assertThat(throwable).hasCause(new IllegalArgumentException("bad arg")); assertThat(throwable).hasCause(new NullPointerException()); assertThat(throwable).hasCause(null); // prefer hasNoCause()
- Parameters:
cause
- the expected cause- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has not the given cause.
-
hasCauseReference
Verifies that the actualThrowable
has a cause that refers to the given one, i.e. using == comparisonExample:
Throwable invalidArgException = new IllegalArgumentException("invalid arg"); Throwable throwable = new Throwable(invalidArgException); // This assertion succeeds: assertThat(throwable).hasCauseReference(invalidArgException); // These assertions fail: assertThat(throwable).hasCauseReference(new IllegalArgumentException("invalid arg")); assertThat(throwable).hasCauseReference(new NullPointerException()); assertThat(throwable).hasCauseReference(null); // prefer hasNoCause()
- Parameters:
expected
- the expected cause- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has a cause that does not refer to the given (i.e. actual.getCause() != cause)
-
hasNoCause
Verifies that the actualThrowable
does not have a cause.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has a cause.
-
cause
Returns a new assertion object that uses the cause of the current Throwable as the actual Throwable under test.Examples:
Throwable cause = new IllegalArgumentException("wrong amount 123"); Throwable exception = new Exception("boom!", cause); // typical use: assertThat(throwableWithMessage).cause() .hasMessageStartingWith("wrong amount");
- Returns:
- a new assertion object
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
does not have a cause.- Since:
- 3.23.0
-
getCause
Deprecated.usecause()
instead.Returns a new assertion object that uses the cause of the current Throwable as the actual Throwable under test.
Examples:
Throwable cause = new IllegalArgumentException("wrong amount 123"); Throwable exception = new Exception("boom!", cause); // typical use: assertThat(throwableWithMessage).getCause() .hasMessageStartingWith("wrong amount");
- Returns:
- a new assertion object
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
does not have a cause.- Since:
- 3.16.0
-
rootCause
Returns a new assertion object that uses the root cause of the current Throwable as the actual Throwable under test.Examples:
Throwable rootCause = new JdbcException("invalid query"); Throwable cause = new RuntimeException(rootCause); Throwable exception = new Exception("boom!", cause); // typical use: assertThat(throwableWithMessage).rootCause() .hasMessageStartingWith("invalid");
- Returns:
- a new assertion object
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
does not have a root cause.- Since:
- 3.23.0
-
getRootCause
Deprecated.userootCause()
instead.Returns a new assertion object that uses the root cause of the current Throwable as the actual Throwable under test.
Examples:
Throwable rootCause = new JdbcException("invalid query"); Throwable cause = new RuntimeException(rootCause); Throwable exception = new Exception("boom!", cause); // typical use: assertThat(throwableWithMessage).getRootCause() .hasMessageStartingWith("invalid");
- Returns:
- a new assertion object
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
does not have a root cause.- Since:
- 3.16.0
-
hasMessageStartingWith
Verifies that the message of the actualThrowable
starts with the given description.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion will pass: assertThat(throwableWithMessage).hasMessageStartingWith("wrong amount"); // assertions will fail: assertThat(throwableWithMessage).hasMessageStartingWith("right amount");
- Parameters:
description
- the description expected to start the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not start with the given description.
-
hasMessageStartingWith
Verifies that the message of the actualThrowable
starts with the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion will pass: assertThat(throwableWithMessage).hasMessageStartingWith("%s amount", "wrong"); // assertions will fail: assertThat(throwableWithMessage).hasMessageStartingWith("%s amount", "right");
- Parameters:
description
- the description expected to start the actualThrowable
's message.parameters
- argument referenced by the format specifiers in the format string- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not start with the given description.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasMessageContaining
Verifies that the message of the actualThrowable
contains the given description.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); Throwable throwableWithoutMessage = new IllegalArgumentException(); // assertion will pass: assertThat(throwableWithMessage).hasMessageContaining("123"); // assertions will fail: assertThat(throwableWithoutMessage).hasMessageContaining("123"); assertThat(throwableWithMessage).hasMessageContaining("234");
- Parameters:
description
- the description expected to be contained in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not contain the given description.
-
hasMessageContaining
Verifies that the message of the actualThrowable
contains the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); Throwable throwableWithoutMessage = new IllegalArgumentException(); // assertion will pass: assertThat(throwableWithMessage).hasMessageContaining("amount %d", 123); // assertions will fail: assertThat(throwableWithoutMessage).hasMessageContaining("amount %d", 123); assertThat(throwableWithMessage).hasMessageContaining("%s amount", "right");
- Parameters:
description
- the description expected to be contained in the actualThrowable
's message.parameters
- argument referenced by the format specifiers in the format string- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not contain the given description.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasMessageContainingAll
Verifies that the message of the actualThrowable
contains all the given values.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); Throwable throwableWithoutMessage = new IllegalArgumentException(); // assertion will pass: assertThat(throwableWithMessage).hasMessageContainingAll("amount", "123"); // assertions will fail: assertThat(throwableWithoutMessage).hasMessageContainingAll("123"); assertThat(throwableWithMessage).hasMessageContainingAll("234");
- Parameters:
values
- the Strings expected to be contained in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not contain all the given values.
-
hasMessageNotContaining
Verifies that the message of the actualThrowable
does not contain the given content or isnull
.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); Throwable throwableWithoutMessage = new IllegalArgumentException(); // assertions will pass: assertThat(throwableWithMessage).hasMessageNotContaining("234"); assertThat(throwableWithoutMessage).hasMessageNotContaining("foo"); // assertion will fail: assertThat(throwableWithMessage).hasMessageNotContaining("amount");
- Parameters:
content
- the content expected not to be contained in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
contains the given content.- Since:
- 3.12.0
-
hasMessageNotContainingAny
Verifies that the message of the actualThrowable
does not contain any of the given values or isnull
.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); Throwable throwableWithoutMessage = new IllegalArgumentException(); // assertions will pass: assertThat(throwableWithMessage).hasMessageNotContainingAny("234"); assertThat(throwableWithoutMessage).hasMessageNotContainingAny("foo"); // assertion will fail: assertThat(throwableWithMessage).hasMessageNotContainingAny("foo", "amount");
- Parameters:
values
- the contents expected to not be contained in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
contains any of the given values.- Since:
- 3.12.0
-
hasStackTraceContaining
Verifies that the stack trace of the actualThrowable
contains the given description.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion will pass assertThat(throwableWithMessage).hasStackTraceContaining("amount 123"); // assertion will fail assertThat(throwableWithMessage).hasStackTraceContaining("456");
- Parameters:
description
- the description expected to be contained in the actualThrowable
's stack trace.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the stack trace of the actualThrowable
does not contain the given description.
-
hasStackTraceContaining
Verifies that the stack trace of the actualThrowable
contains the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion succeeds assertThat(throwableWithMessage).hasStackTraceContaining("%s", amount); // assertion fails assertThat(throwableWithMessage).hasStackTraceContaining("%d", 456);
- Parameters:
description
- the description expected to be contained in the actualThrowable
's stack trace.parameters
- argument referenced by the format specifiers in the format string- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the stack trace of the actualThrowable
does not contain the given description.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasMessageMatching
Verifies that the message of theThrowable
under test matches the given regular expression.Examples:
Throwable throwable = new IllegalArgumentException("wrong amount 123"); // assertion succeeds assertThat(throwable).hasMessageMatching("wrong amount [0-9]*"); // assertion fails assertThat(throwable).hasMessageMatching("wrong amount [0-9]* euros");
- Parameters:
regex
- the regular expression of value expected to be matched the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not match the given regular expression.NullPointerException
- if the regex is null
-
hasMessageMatching
Verifies that the message of theThrowable
under test matches the given regular expressionPattern
.Examples:
Throwable throwable = new IllegalArgumentException("wrong amount 123"); // assertion succeeds assertThat(throwable).hasMessageMatching(Pattern.compile("wrong amount [0-9]*")); // assertion fails assertThat(throwable).hasMessageMatching(Pattern.compile("wrong amount [0-9]* euros"));
- Parameters:
regex
- the regular expression of value expected to be matched the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not match the given regular expression.NullPointerException
- if the regex is null
-
hasMessageFindingMatch
Verifies that a sequence of the message of the actualThrowable
matches with the given regular expression (seeMatcher.find()
).
ThePattern
used under the hood enables thePattern.DOTALL
mode.Examples:
Throwable throwable = new IllegalArgumentException("Dear John,\n" + "it' s a wrong amount"); // assertion will pass assertThat(throwable).hasMessageFindingMatch("wrong amount"); assertThat(throwable).hasMessageFindingMatch("Dear John"); assertThat(throwable).hasMessageFindingMatch("wrong amount$"); // assertion will fail assertThat(throwable).hasMessageFindingMatch("Dear John$");
- Parameters:
regex
- the regular expression expected to be found in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
doesn't contain any sequence matching with the given regular expressionNullPointerException
- if the regex is null- Since:
- 3.12.0
-
hasMessageEndingWith
Verifies that the message of the actualThrowable
ends with the given description.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion will pass assertThat(throwableWithMessage).hasMessageEndingWith("123"); // assertion will fail assertThat(throwableWithMessage).hasMessageEndingWith("456");
- Parameters:
description
- the description expected to end the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not end with the given description.
-
hasMessageEndingWith
Verifies that the message of the actualThrowable
ends with the given description, after being formatted using theString.format(java.lang.String, java.lang.Object...)
method.Examples:
Throwable throwableWithMessage = new IllegalArgumentException("wrong amount 123"); // assertion will pass assertThat(throwableWithMessage).hasMessageEndingWith("%s 123", "amount"); // assertion will fail assertThat(throwableWithMessage).hasMessageEndingWith("amount %d", 456);
- Parameters:
description
- the description expected to end the actualThrowable
's message.parameters
- argument referenced by the format specifiers in the format string- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the message of the actualThrowable
does not end with the given description.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasCauseInstanceOf
Verifies that the cause of the actualThrowable
is an instance of the given type.Example:
Throwable throwable = new Throwable(new NullPointerException()); // assertions will pass assertThat(throwable).hasCauseInstanceOf(NullPointerException.class); assertThat(throwable).hasCauseInstanceOf(RuntimeException.class); // assertion will fail assertThat(throwable).hasCauseInstanceOf(IllegalArgumentException.class);
- Parameters:
type
- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if given type isnull
.AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has no cause.AssertionError
- if the cause of the actualThrowable
is not an instance of the given type.
-
hasCauseExactlyInstanceOf
Verifies that the cause of the actualThrowable
is exactly an instance of the given type.Example:
Throwable throwable = new Throwable(new NullPointerException()); // assertion will pass assertThat(throwable).hasCauseExactlyInstanceOf(NullPointerException.class); // assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match) assertThat(throwable).hasCauseExactlyInstanceOf(RuntimeException.class); assertThat(throwable).hasCauseExactlyInstanceOf(IllegalArgumentException.class);
- Parameters:
type
- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if given type isnull
.AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has no cause.AssertionError
- if the cause of the actualThrowable
is not exactly an instance of the given type.
-
hasRootCause
Verifies that the actualThrowable
has a root cause similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).Example:
Throwable invalidArgException = new IllegalArgumentException("invalid arg"); Throwable throwable = new Throwable(new RuntimeException(invalidArgException)); // This assertion succeeds: assertThat(throwable).hasRootCause(invalidArgException); // These assertions fail: assertThat(throwable).hasRootCause(new IllegalArgumentException("bad arg")); assertThat(throwable).hasRootCause(new RuntimeException()); assertThat(throwable).hasRootCause(null); // prefer hasNoCause()
- Parameters:
cause
- the expected root cause- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has not the given cause.- Since:
- 3.12.0
-
hasRootCauseInstanceOf
Verifies that the root cause of the actualThrowable
is an instance of the given type.Example:
Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException())); // assertions will pass assertThat(throwable).hasRootCauseInstanceOf(NullPointerException.class); assertThat(throwable).hasRootCauseInstanceOf(RuntimeException.class); // assertion will fail assertThat(throwable).hasRootCauseInstanceOf(IllegalStateException.class);
- Parameters:
type
- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if given type isnull
.AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has no cause.AssertionError
- if the cause of the actualThrowable
is not an instance of the given type.
-
hasRootCauseExactlyInstanceOf
Verifies that the root cause of the actualThrowable
is exactly an instance of the given type.Example:
Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException())); // assertion will pass assertThat(throwable).hasRootCauseExactlyInstanceOf(NullPointerException.class); // assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match) assertThat(throwable).hasRootCauseExactlyInstanceOf(RuntimeException.class); assertThat(throwable).hasRootCauseExactlyInstanceOf(IllegalStateException.class);
- Parameters:
type
- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException
- if given type isnull
.AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
has no cause.AssertionError
- if the root cause of the actualThrowable
is not exactly an instance of the given type.
-
hasRootCauseMessage
Verifies that the message of the root cause of the actualThrowable
is equal to the given one.Example:
Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException("object"))); // assertion will pass assertThat(throwable).hasRootCauseMessage("object"); // assertions will fail assertThat((Throwable) null).hasRootCauseMessage("object"); assertThat(throwable).hasRootCauseMessage("another object"); assertThat(new Throwable()).hasRootCauseMessage("object"); assertThat(new Throwable(new NullPointerException())).hasRootCauseMessage("object");
- Parameters:
message
- the expected root cause message.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the root cause of the actualThrowable
isnull
.AssertionError
- if the message of the root cause of the actualThrowable
is not equal to the given one.- Since:
- 3.14.0
-
hasRootCauseMessage
Verifies that the message of the root cause of the actualThrowable
is equal to the given one, after being formatted usingString.format(String, Object...)
method.Example:
Throwable throwable = new Throwable(new IllegalStateException(new NullPointerException("expected message"))); // assertion will pass assertThat(throwable).hasRootCauseMessage("%s %s", "expected", "message"); // assertions will fail assertThat((Throwable) null).hasRootCauseMessage("%s %s", "expected", "message"); assertThat(throwable).hasRootCauseMessage("%s", "message"); assertThat(new Throwable()).hasRootCauseMessage("%s %s", "expected", "message"); assertThat(new Throwable(new NullPointerException())).hasRootCauseMessage("%s %s", "expected", "message");
- Parameters:
message
- the expected root cause message.parameters
- argument referenced by the format specifiers in the format string.- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the root cause of the actualThrowable
isnull
.AssertionError
- if the message of the root cause of the actualThrowable
is not equal to the given one.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.- Since:
- 3.14.0
-
hasNoSuppressedExceptions
Verifies that the actualThrowable
has no suppressed exceptions.Example:
// assertion will pass assertThat(new Throwable()).hasNoSuppressedExceptions(); // assertion will fail Throwable throwableWithSuppressedException = new Throwable(); throwableWithSuppressedException.addSuppressed(new IllegalArgumentException()); assertThat(throwableWithSuppressedException).hasNoSuppressedExceptions();
- Returns:
- this assertion object.
- Throws:
NullPointerException
- if given type isnull
.AssertionError
- if the actualThrowable
has any suppressed exceptions.- Since:
- 2.6.0 / 3.6.0
-
hasSuppressedException
Verifies that the actualThrowable
has a suppressed exception similar to the given one, that is with the same type and message (it does not use theequals
method for comparison).Example:
Throwable throwable = new Throwable(); Throwable invalidArgException = new IllegalArgumentException("invalid argument"); throwable.addSuppressed(invalidArgException); // These assertions succeed: assertThat(throwable).hasSuppressedException(invalidArgException); assertThat(throwable).hasSuppressedException(new IllegalArgumentException("invalid argument")); // These assertions fail: assertThat(throwable).hasSuppressedException(new IllegalArgumentException("invalid parameter")); assertThat(throwable).hasSuppressedException(new NullPointerException());
- Parameters:
suppressedException
- the expected suppressed exception- Returns:
- this assertion object.
- Throws:
AssertionError
- if the actualThrowable
isnull
.AssertionError
- if the actualThrowable
does not have the given suppressed exception.- Since:
- 2.6.0 / 3.6.0
-
doesNotThrowAnyException
public void doesNotThrowAnyException()Verifies that theThrowableAssert.ThrowingCallable
didn't raise a throwable.Example :
assertThatCode(() -> foo.bar()).doesNotThrowAnyException();
- Throws:
AssertionError
- if the actual statement raised aThrowable
.- Since:
- 3.7.0
-
message
A shortcut forextracting(Throwable::getMessage, as(InstanceOfAssertFactories.STRING))
which allows to extract a throwable's message and then execute assertions on it.Note that once you have navigated to the throwable's message you can't navigate back to the throwable.
Example :
Throwable throwable = new Throwable("boom!"); // assertions succeed: assertThat(throwable).message().startsWith("boo") .endsWith("!"); // assertion fails: assertThat(throwable).message().isEmpty();
- Returns:
- the created
StringAssert
on the throwable message. - Since:
- 3.22.0
-
cause()
instead.