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.public abstract class AbstractThrowableAssert<SELF extends AbstractThrowableAssert<SELF,ACTUAL>,ACTUAL extends java.lang.Throwable> extends AbstractObjectAssert<SELF,ACTUAL>
Throwables.| Modifier and Type | Field and Description |
|---|---|
(package private) Throwables |
throwables |
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals| Constructor and Description |
|---|
AbstractThrowableAssert(ACTUAL actual,
java.lang.Class<?> selfType) |
| Modifier and Type | Method and Description |
|---|---|
void |
doesNotThrowAnyException()
Verifies that the
ThrowableAssert.ThrowingCallable didn't raise a throwable. |
protected SELF |
hasBeenThrown() |
SELF |
hasCause(java.lang.Throwable cause)
Verifies that the actual
Throwable has a cause similar to the given one, that is with the same type and message
(it does not use the equals method for comparison). |
SELF |
hasCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actual
Throwable is exactly an instance of the given type. |
SELF |
hasCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actual
Throwable is an instance of the given type. |
SELF |
hasCauseReference(java.lang.Throwable expected)
Verifies that the actual
Throwable has a cause that refers to the given one, i.e. |
SELF |
hasMessage(java.lang.String message)
Verifies that the message of the actual
Throwable is equal to the given one. |
SELF |
hasMessage(java.lang.String message,
java.lang.Object... parameters)
Verifies that the message of the actual (@code Throwable) is equal to the given one, after being formatted using
the
String.format(java.lang.String, java.lang.Object...) method. |
SELF |
hasMessageContaining(java.lang.String description)
Verifies that the message of the actual
Throwable contains the given description. |
SELF |
hasMessageContainingAll(java.lang.CharSequence... values)
Verifies that the message of the actual
Throwable contains all the given values. |
SELF |
hasMessageEndingWith(java.lang.String description)
Verifies that the message of the actual
Throwable ends with the given description. |
SELF |
hasMessageFindingMatch(java.lang.String regex)
Verifies that a sequence of the message of the actual
Throwable matches with
the given regular expression (see Matcher.find()).The Pattern used under the hood enables the Pattern.DOTALL mode. |
SELF |
hasMessageMatching(java.lang.String regex)
Verifies that the message of the actual
Throwable matches the given regular expression. |
SELF |
hasMessageNotContaining(java.lang.String content)
Verifies that the message of the actual
Throwable does not contain the given content or is null. |
SELF |
hasMessageNotContainingAny(java.lang.CharSequence... values)
Verifies that the message of the actual
Throwable does not contain any of the given values or is null. |
SELF |
hasMessageStartingWith(java.lang.String description)
Verifies that the message of the actual
Throwable starts with the given description. |
SELF |
hasNoCause()
Verifies that the actual
Throwable does not have a cause. |
SELF |
hasNoSuppressedExceptions()
Verifies that the actual
Throwable has no suppressed exceptions. |
SELF |
hasRootCause(java.lang.Throwable cause)
Verifies that the actual
Throwable has a root cause similar to the given one, that is with the same type and message
(it does not use the equals method for comparison). |
SELF |
hasRootCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the root cause of the actual
Throwable is exactly an instance of the given type. |
SELF |
hasRootCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the root cause of the actual
Throwable is an instance of the given type. |
SELF |
hasStackTraceContaining(java.lang.String description)
Verifies that the stack trace of the actual
Throwable contains the given description. |
SELF |
hasSuppressedException(java.lang.Throwable suppressedException)
Verifies that the actual
Throwable has a suppressed exception similar to the given one, that is with the same type and message
(it does not use the equals method for comparison). |
as, as, extracting, extracting, extracting, extracting, getComparatorsByType, hasAllNullFieldsOrProperties, hasAllNullFieldsOrPropertiesExcept, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, newObjectAssert, returns, usingComparatorForFields, usingComparatorForType, usingRecursiveComparison, usingRecursiveComparison, withAssertionState, withComparatorByPropertyOrField, withTypeComparatorasInstanceOf, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, hasToString, inBinary, inHexadecimal, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isInstanceOfSatisfying, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, matches, matches, newListAssertInstance, overridingErrorMessage, satisfies, satisfies, satisfiesAnyOf, satisfiesAnyOf, setCustomRepresentation, throwAssertionError, usingComparator, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnErrorThrowables throwables
public AbstractThrowableAssert(ACTUAL actual, java.lang.Class<?> selfType)
protected SELF hasBeenThrown()
public SELF hasMessage(java.lang.String message)
Throwable is equal to the given one.message - the expected message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable is not equal to the given one.public SELF hasMessage(java.lang.String message, java.lang.Object... parameters)
String.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");message - a format string representing the expected messageparameters - argument referenced by the format specifiers in the format stringjava.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable is not equal to the given one.java.util.IllegalFormatException - if the message contains an illegal syntax according to String.format(String, Object...).public SELF hasCause(java.lang.Throwable cause)
Throwable has a cause similar to the given one, that is with the same type and message
(it does not use the equals 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()cause - the expected causejava.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has not the given cause.public SELF hasCauseReference(java.lang.Throwable expected)
Throwable has a cause that refers to the given one, i.e. using == comparison
Example:
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()expected - the expected causejava.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has a cause that does not refer to the given (i.e. actual.getCause() != cause)public SELF hasNoCause()
Throwable does not have a cause.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has a cause.public SELF hasMessageStartingWith(java.lang.String description)
Throwable starts with the given description.description - the description expected to start the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable does not start with the given description.public SELF hasMessageContaining(java.lang.String description)
Throwable 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"); description - the description expected to be contained in the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable does not contain the given description.public SELF hasMessageContainingAll(java.lang.CharSequence... values)
Throwable 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"); values - the Strings expected to be contained in the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable does not contain all the given values.public SELF hasMessageNotContaining(java.lang.String content)
Throwable does not contain the given content or is null.
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");content - the content expected not to be contained in the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable contains the given content.public SELF hasMessageNotContainingAny(java.lang.CharSequence... values)
Throwable does not contain any of the given values or is null.
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");values - the contents expected to not be contained in the actual Throwables's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable contains any of the given values.public SELF hasStackTraceContaining(java.lang.String description)
Throwable contains the given description.description - the description expected to be contained in the actual Throwable's stack trace.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the stack trace of the actual Throwable does not contain the given description.public SELF hasMessageMatching(java.lang.String regex)
Throwable matches the given regular expression.
Examples:
Throwable throwable = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThat(throwable).hasMessageMatching("wrong amount [0-9]*");
// assertion will fail
assertThat(throwable).hasMessageMatching("wrong amount [0-9]* euros");regex - the regular expression of value expected to be matched the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable does not match the given regular expression.java.lang.NullPointerException - if the regex is nullpublic SELF hasMessageFindingMatch(java.lang.String regex)
Throwable matches with
the given regular expression (see Matcher.find()).Pattern used under the hood enables the Pattern.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$");regex - the regular expression expected to be found in the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable doesn't contain any sequence matching with the given regular expressionjava.lang.NullPointerException - if the regex is nullpublic SELF hasMessageEndingWith(java.lang.String description)
Throwable ends with the given description.description - the description expected to end the actual Throwable's message.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the message of the actual Throwable does not end with the given description.public SELF hasCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Throwable 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);type - the expected cause type.java.lang.NullPointerException - if given type is null.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has no cause.java.lang.AssertionError - if the cause of the actual Throwable is not an instance of the given type.public SELF hasCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Throwable 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);type - the expected cause type.java.lang.NullPointerException - if given type is null.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has no cause.java.lang.AssertionError - if the cause of the actual Throwable is not exactly an instance of the given
type.public SELF hasRootCause(java.lang.Throwable cause)
Throwable has a root cause similar to the given one, that is with the same type and message
(it does not use the equals 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()cause - the expected root causejava.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has not the given cause.public SELF hasRootCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Throwable 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);type - the expected cause type.java.lang.NullPointerException - if given type is null.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has no cause.java.lang.AssertionError - if the cause of the actual Throwable is not an instance of the given type.public SELF hasRootCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Throwable 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);type - the expected cause type.java.lang.NullPointerException - if given type is null.java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has no cause.java.lang.AssertionError - if the root cause of the actual Throwable is not exactly an instance of the
given type.public SELF hasNoSuppressedExceptions()
Throwable 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();java.lang.NullPointerException - if given type is null.java.lang.AssertionError - if the actual Throwable has any suppressed exceptions.public SELF hasSuppressedException(java.lang.Throwable suppressedException)
Throwable has a suppressed exception similar to the given one, that is with the same type and message
(it does not use the equals 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());suppressedException - the expected suppressed exceptionjava.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable does not have the given suppressed exception.public void doesNotThrowAnyException()
ThrowableAssert.ThrowingCallable didn't raise a throwable.
Example :
assertThatCode(() -> foo.bar()).doesNotThrowAnyException();java.lang.AssertionError - if the actual statement raised a Throwable.