public class ThrowableAssertAlternative<T extends java.lang.Throwable> extends AbstractAssert<ThrowableAssertAlternative<T>,T>
Throwable similar to ThrowableAssert but with assertions methods named
differently to make testing code fluent (ex : withMessage instead of hasMessage.
assertThatExceptionOfType(IOException.class)
.isThrownBy(() -> { throw new IOException("boom! tcha!"); });
.withMessage("boom! %s", "tcha!");
This class is linked with the ThrowableTypeAssert and allow to check that an exception
type is thrown by a lambda.
| Modifier and Type | Field and Description |
|---|---|
private ThrowableAssert |
delegate |
actual, conditions, info, myself, objects| Constructor and Description |
|---|
ThrowableAssertAlternative(T actual) |
| Modifier and Type | Method and Description |
|---|---|
ThrowableAssertAlternative<T> |
withCause(java.lang.Throwable cause)
Verifies that the actual
Throwable has a cause similar to the given one, that is with same type and message
(it does not use equals method for comparison). |
ThrowableAssertAlternative<T> |
withCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actual
Throwable is exactly an instance of the given type. |
ThrowableAssertAlternative<T> |
withCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actual
Throwable is an instance of the given type. |
ThrowableAssertAlternative<T> |
withMessage(java.lang.String message)
Verifies that the message of the actual
Throwable is equal to the given one. |
ThrowableAssertAlternative<T> |
withMessage(java.lang.String message,
java.lang.Object... parameters)
Verifies that the message of the actual
Throwable is equal to the given one built using String.format(String, Object...) syntax. |
ThrowableAssertAlternative<T> |
withMessageContaining(java.lang.String description)
Verifies that the message of the actual
Throwable contains with the given description. |
ThrowableAssertAlternative<T> |
withMessageEndingWith(java.lang.String description)
Verifies that the message of the actual
Throwable ends with the given description. |
ThrowableAssertAlternative<T> |
withMessageMatching(java.lang.String regex)
Verifies that the message of the actual
Throwable matches with the given regular expression. |
ThrowableAssertAlternative<T> |
withMessageStartingWith(java.lang.String description)
Verifies that the message of the actual
Throwable starts with the given description. |
ThrowableAssertAlternative<T> |
withNoCause()
Verifies that the actual
Throwable does not have a cause. |
ThrowableAssertAlternative<T> |
withRootCauseExactlyInstanceOf(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. |
ThrowableAssertAlternative<T> |
withRootCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the root cause of the actual
Throwable is an instance of the given type. |
ThrowableAssertAlternative<T> |
withStackTraceContaining(java.lang.String description)
Verifies that the stack trace of the actual
Throwable contains with the given description. |
as, as, asList, asString, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, 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, overridingErrorMessage, satisfies, setCustomRepresentation, throwAssertionError, usingComparator, usingDefaultComparator, withFailMessage, withRepresentation, withThreadDumpOnErrorprivate ThrowableAssert delegate
ThrowableAssertAlternative(T actual)
public ThrowableAssertAlternative<T> withMessage(java.lang.String message)
Throwable is equal to the given one.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount 123");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount 123 euros");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.AbstractThrowableAssert.hasMessage(String)public ThrowableAssertAlternative<T> withMessage(java.lang.String message, java.lang.Object... parameters)
Throwable is equal to the given one built using String.format(String, Object...) syntax.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount %s, "123");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount 123 euros");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.AbstractThrowableAssert.hasMessage(String)public ThrowableAssertAlternative<T> withCause(java.lang.Throwable cause)
Throwable has a cause similar to the given one, that is with same type and message
(it does not use equals method for comparison).
Example:
Throwable illegalArgumentException = new IllegalArgumentException("invalid arg");
Throwable wrappingException = new Throwable(illegalArgumentException);
// This assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw wrappingException;})
.withCause(illegalArgumentException);
// These assertions fail:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw wrappingException;})
.withCause(new IllegalArgumentException("bad arg"));
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw wrappingException;})
.withCause(new NullPointerException());
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw wrappingException;})
.withCause(null);java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has not the given cause.AbstractThrowableAssert.hasCause(Throwable)public ThrowableAssertAlternative<T> withNoCause()
Throwable does not have a cause.
Example:
IllegalArgumentException exception = new IllegalArgumentException();
// This assertion succeeds:
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> {throw exception;})
.withNoCause();
// These assertion fails:
Throwable illegalArgumentException = new Throwable(exception);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withNoCause();java.lang.AssertionError - if the actual Throwable is null.java.lang.AssertionError - if the actual Throwable has a cause.AbstractThrowableAssert.hasNoCause()public ThrowableAssertAlternative<T> withMessageStartingWith(java.lang.String description)
Throwable starts with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("wrong amount");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("right amount");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.AbstractThrowableAssert.hasMessageStartingWith(String)public ThrowableAssertAlternative<T> withMessageContaining(java.lang.String description)
Throwable contains with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("amount");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("456");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.AbstractThrowableAssert.hasMessageContaining(String)public ThrowableAssertAlternative<T> withStackTraceContaining(java.lang.String description)
Throwable contains with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("amount");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("456");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.AbstractThrowableAssert.hasStackTraceContaining(String)public ThrowableAssertAlternative<T> withMessageMatching(java.lang.String regex)
Throwable matches with the given regular expression.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageMatching("wrong amount [0-9]*");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageMatching("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 nullAbstractThrowableAssert.hasMessageMatching(String)public ThrowableAssertAlternative<T> withMessageEndingWith(java.lang.String description)
Throwable ends with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("123");
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("456");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.AbstractThrowableAssert.hasMessageEndingWith(String)public ThrowableAssertAlternative<T> withCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Throwable is an instance of the given type.
Example:
Throwable throwable = new Throwable(new NullPointerException());
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(NullPointerException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(RuntimeException.class);
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(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.AbstractThrowableAssert.hasCauseInstanceOf(Class)public ThrowableAssertAlternative<T> withCauseExactlyInstanceOf(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
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseExactlyInstanceOf(NullPointerException.class);
// assertions will fail (even if NullPointerException is a RuntimeException since we want an exact match)
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseExactlyInstanceOf(RuntimeException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseExactlyInstanceOf(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.AbstractThrowableAssert.hasCauseExactlyInstanceOf(Class)public ThrowableAssertAlternative<T> withRootCauseInstanceOf(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()));
// assertion will pass
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(NullPointerException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(RuntimeException.class);
// assertion will fail
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(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.AbstractThrowableAssert.hasRootCauseInstanceOf(Class)public ThrowableAssertAlternative<T> withRootCauseExactlyInstanceOf(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
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseExactlyInstanceOf(NullPointerException.class);
// assertion will fail (even if NullPointerException is a RuntimeException since we want an exact match)
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseExactlyInstanceOf(RuntimeException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseExactlyInstanceOf(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.AbstractThrowableAssert.hasRootCauseExactlyInstanceOf(Class)