public class ThrowableAssertAlternative<ACTUAL extends Throwable> extends AbstractObjectAssert<ThrowableAssertAlternative<ACTUAL>,ACTUAL>
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.actual, info, myself, objects, throwUnsupportedExceptionOnEquals| Constructor and Description |
|---|
ThrowableAssertAlternative(ACTUAL actual) |
| Modifier and Type | Method and Description |
|---|---|
ThrowableAssertAlternative<ACTUAL> |
as(Description description)
Sets the description of the assertion that is going to be called after.
|
ThrowableAssertAlternative<ACTUAL> |
describedAs(Description description)
Sets the description of the assertion that is going to be called after.
|
ThrowableAssertAlternative<ACTUAL> |
describedAs(String description,
Object... args)
Sets the description of the assertion that is going to be called after.
|
protected ThrowableAssert<ACTUAL> |
getDelegate() |
ThrowableAssertAlternative<?> |
havingCause()
|
ThrowableAssertAlternative<?> |
havingRootCause()
|
ThrowableAssertAlternative<ACTUAL> |
withCause(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<ACTUAL> |
withCauseExactlyInstanceOf(Class<? extends Throwable> type)
Verifies that the cause of the actual
Throwable is exactly an instance of the given type. |
ThrowableAssertAlternative<ACTUAL> |
withCauseInstanceOf(Class<? extends Throwable> type)
Verifies that the cause of the actual
Throwable is an instance of the given type. |
ThrowableAssertAlternative<ACTUAL> |
withMessage(String message)
Verifies that the message of the actual
Throwable is equal to the given one. |
ThrowableAssertAlternative<ACTUAL> |
withMessage(String message,
Object... parameters)
Verifies that the message of the actual
Throwable is equal to the given one built using String.format(String, Object...) syntax. |
ThrowableAssertAlternative<ACTUAL> |
withMessageContaining(String description)
Verifies that the message of the actual
Throwable contains the given description. |
ThrowableAssertAlternative<ACTUAL> |
withMessageContaining(String description,
Object... parameters)
Verifies that the message of the actual
Throwable contains the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method. |
ThrowableAssertAlternative<ACTUAL> |
withMessageContainingAll(CharSequence... values)
Verifies that the message of the actual
Throwable contains all the given values. |
ThrowableAssertAlternative<ACTUAL> |
withMessageEndingWith(String description)
Verifies that the message of the actual
Throwable ends with the given description. |
ThrowableAssertAlternative<ACTUAL> |
withMessageEndingWith(String description,
Object... parameters)
Verifies that the message of the actual
Throwable ends with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method. |
ThrowableAssertAlternative<ACTUAL> |
withMessageMatching(String regex)
Verifies that the message of the actual
Throwable matches with the given regular expression. |
ThrowableAssertAlternative<ACTUAL> |
withMessageNotContaining(String content)
Verifies that the message of the actual
Throwable does not contain the given content or is null. |
ThrowableAssertAlternative<ACTUAL> |
withMessageNotContainingAny(CharSequence... values)
Verifies that the message of the actual
Throwable does not contain any of the given values or is null. |
ThrowableAssertAlternative<ACTUAL> |
withMessageStartingWith(String description)
Verifies that the message of the actual
Throwable starts with the given description. |
ThrowableAssertAlternative<ACTUAL> |
withMessageStartingWith(String description,
Object... parameters)
Verifies that the message of the actual
Throwable starts with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method. |
ThrowableAssertAlternative<ACTUAL> |
withNoCause()
Verifies that the actual
Throwable does not have a cause. |
ThrowableAssertAlternative<ACTUAL> |
withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)
Verifies that the root cause of the actual
Throwable is exactly an instance of the given type. |
ThrowableAssertAlternative<ACTUAL> |
withRootCauseInstanceOf(Class<? extends Throwable> type)
Verifies that the root cause of the actual
Throwable is an instance of the given type. |
ThrowableAssertAlternative<ACTUAL> |
withStackTraceContaining(String description)
Verifies that the stack trace of the actual
Throwable contains with the given description. |
ThrowableAssertAlternative<ACTUAL> |
withStackTraceContaining(String description,
Object... parameters)
Verifies that the stack trace of the actual
Throwable contains with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method. |
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, usingRecursiveComparison, usingRecursiveComparisonareEqual, asInstanceOf, asList, assertionError, asString, descriptionText, doesNotHave, doesNotHaveSameClassAs, doesNotHaveSameHashCodeAs, doesNotHaveToString, equals, extracting, extracting, failure, failureWithActualExpected, failWithActualExpectedAndMessage, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, hasSameHashCodeAs, 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, withThreadDumpOnErrorclone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitaspublic ThrowableAssertAlternative(ACTUAL actual)
protected ThrowableAssert<ACTUAL> getDelegate()
public ThrowableAssertAlternative<ACTUAL> as(Description description)
DescriptableYou must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a String by allowing
users to pass their own implementation of a description. For example, a description that creates its value lazily,
only when an assertion failure occurs.
as in interface Descriptable<ThrowableAssertAlternative<ACTUAL extends Throwable>>as in class AbstractObjectAssert<ThrowableAssertAlternative<ACTUAL extends Throwable>,ACTUAL extends Throwable>description - the new description to set.this object.Descriptable.describedAs(Description)public ThrowableAssertAlternative<ACTUAL> withMessage(String message)
Throwable is equal to the given one.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount 123");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount 123 euros");message - the expected message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable is not equal to the given one.AbstractThrowableAssert.hasMessage(String)public ThrowableAssertAlternative<ACTUAL> withMessage(String message, 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 succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessage("wrong amount %s, "123");
// assertion fails:
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 stringAssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable is not equal to the given one.AbstractThrowableAssert.hasMessage(String)public ThrowableAssertAlternative<ACTUAL> withCause(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);cause - the expected cause.AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has not the given cause.AbstractThrowableAssert.hasCause(Throwable)public ThrowableAssertAlternative<ACTUAL> 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();AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has a cause.AbstractThrowableAssert.hasNoCause()public ThrowableAssertAlternative<ACTUAL> withMessageStartingWith(String description)
Throwable starts with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("wrong amount");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("right amount");description - the description expected to start the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not start with the given description.AbstractThrowableAssert.hasMessageStartingWith(String)public ThrowableAssertAlternative<ACTUAL> withMessageStartingWith(String description, Object... parameters)
Throwable starts with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("%s amount", "wrong");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageStartingWith("%s amount", "right");description - the description expected to start the actual Throwable's message.parameters - argument referenced by the format specifiers in the format stringAssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not start with the given description.IllegalFormatException - if the message contains an illegal syntax according to String.format(String, Object...).AbstractThrowableAssert.hasMessageStartingWith(String, Object...)public ThrowableAssertAlternative<ACTUAL> withMessageContaining(String description)
Throwable contains the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("amount");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("456");description - the description expected to be contained in the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not contain the given description.AbstractThrowableAssert.hasMessageContaining(String)public ThrowableAssertAlternative<ACTUAL> withMessageContaining(String description, Object... parameters)
Throwable contains the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("%s", amount);
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContaining("%d", 456);description - the description expected to be contained in the actual Throwable's message.parameters - argument referenced by the format specifiers in the format stringAssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not contain the given description.IllegalFormatException - if the message contains an illegal syntax according to String.format(String, Object...).AbstractThrowableAssert.hasMessageContaining(String, Object...)public ThrowableAssertAlternative<ACTUAL> withMessageContainingAll(CharSequence... values)
Throwable contains all the given values.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContainingAll("amount", "123");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageContainingAll("456");values - the Strings expected to be contained in the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not contain all the given values.AbstractThrowableAssert.hasMessageContainingAll(CharSequence...)public ThrowableAssertAlternative<ACTUAL> withMessageNotContaining(String content)
Throwable does not contain the given content or is null.
Examples:
//assertions will pass
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception("boom")))
.withMessageNotContaining("bam");
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception()))
.withMessageNotContaining("bam");
//assertion fails:
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception("boom")))
.withMessageNotContaining("boom");content - the content expected to not be contained in the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable contains the given content.AbstractThrowableAssert.hasMessageNotContaining(String)public ThrowableAssertAlternative<ACTUAL> withMessageNotContainingAny(CharSequence... values)
Throwable does not contain any of the given values or is null.
Examples:
//assertions will pass
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception("boom")))
.withMessageNotContainingAny("bam");
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception()))
.withMessageNotContainingAny("bam");
// assertion fails:
assertThatExceptionOfType(Exception.class)
.isThrownBy(codeThrowing(new Exception("boom")))
.withMessageNotContainingAny("bam", "boom");values - the contents expected to not be contained in the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable contains any of the given values.AbstractThrowableAssert.hasMessageNotContainingAny(CharSequence...)public ThrowableAssertAlternative<ACTUAL> withStackTraceContaining(String description)
Throwable contains with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("amount");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("456");description - the description expected to be contained in the actual Throwable's stack trace.AssertionError - if the actual Throwable is null.AssertionError - if the stack trace of the actual Throwable does not contain the given description.AbstractThrowableAssert.hasStackTraceContaining(String)public ThrowableAssertAlternative<ACTUAL> withStackTraceContaining(String description, Object... parameters)
Throwable contains with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("%s", amount);
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withStackTraceContaining("%d", 456);description - the description expected to be contained in the actual Throwable's stack trace.parameters - argument referenced by the format specifiers in the format stringAssertionError - if the actual Throwable is null.AssertionError - if the stack trace of the actual Throwable does not contain the given description.IllegalFormatException - if the message contains an illegal syntax according to String.format(String, Object...).AbstractThrowableAssert.hasStackTraceContaining(String, Object...)public ThrowableAssertAlternative<ACTUAL> withMessageMatching(String regex)
Throwable matches with the given regular expression.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageMatching("wrong amount [0-9]*");
// assertion fails:
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.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not match the given regular expression.NullPointerException - if the regex is nullAbstractThrowableAssert.hasMessageMatching(String)public ThrowableAssertAlternative<ACTUAL> withMessageEndingWith(String description)
Throwable ends with the given description.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("123");
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("456");description - the description expected to end the actual Throwable's message.AssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not end with the given description.AbstractThrowableAssert.hasMessageEndingWith(String)public ThrowableAssertAlternative<ACTUAL> withMessageEndingWith(String description, Object... parameters)
Throwable ends with the given description, after being formatted using
the String.format(java.lang.String, java.lang.Object...) method.
Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123");
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("%d", 123);
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw illegalArgumentException;})
.withMessageEndingWith("%d", 456);description - the description expected to end the actual Throwable's message.parameters - argument referenced by the format specifiers in the format stringAssertionError - if the actual Throwable is null.AssertionError - if the message of the actual Throwable does not end with the given description.IllegalFormatException - if the message contains an illegal syntax according to String.format(String, Object...).AbstractThrowableAssert.hasMessageEndingWith(String, Object...)public ThrowableAssertAlternative<ACTUAL> withCauseInstanceOf(Class<? extends Throwable> type)
Throwable is an instance of the given type.
Example:
Throwable throwable = new Throwable(new NullPointerException());
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(NullPointerException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(RuntimeException.class);
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withCauseInstanceOf(IllegalArgumentException.class);type - the expected cause type.NullPointerException - if given type is null.AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no cause.AssertionError - if the cause of the actual Throwable is not an instance of the given type.AbstractThrowableAssert.hasCauseInstanceOf(Class)public ThrowableAssertAlternative<ACTUAL> withCauseExactlyInstanceOf(Class<? extends Throwable> type)
Throwable is exactly an instance of the given type.
Example:
Throwable throwable = new Throwable(new NullPointerException());
// assertion succeeds:
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.NullPointerException - if given type is null.AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no cause.AssertionError - if the cause of the actual Throwable is not exactly an instance of the given
type.AbstractThrowableAssert.hasCauseExactlyInstanceOf(Class)public ThrowableAssertAlternative<ACTUAL> withRootCauseInstanceOf(Class<? extends Throwable> type)
Throwable is an instance of the given type.
Example:
Throwable throwable = new Throwable(
new IllegalStateException(
new NullPointerException()));
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(NullPointerException.class);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(RuntimeException.class);
// assertion fails:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseInstanceOf(IllegalStateException.class);type - the expected cause type.NullPointerException - if given type is null.AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no cause.AssertionError - if the cause of the actual Throwable is not an instance of the given type.AbstractThrowableAssert.hasRootCauseInstanceOf(Class)public ThrowableAssertAlternative<ACTUAL> withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)
Throwable is exactly an instance of the given type.
Example:
Throwable throwable = new Throwable(
new IllegalStateException(
new NullPointerException()));
// assertion succeeds:
assertThatExceptionOfType(Throwable.class)
.isThrownBy(() -> {throw throwable;})
.withRootCauseExactlyInstanceOf(NullPointerException.class);
// assertion fails (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.NullPointerException - if given type is null.AssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no cause.AssertionError - if the root cause of the actual Throwable is not exactly an instance of the
given type.AbstractThrowableAssert.hasRootCauseExactlyInstanceOf(Class)public ThrowableAssertAlternative<ACTUAL> describedAs(String description, Object... args)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
Alias for since "as" is a keyword in Groovy.Descriptable.as(String, Object...)
description - the new description to set.args - optional parameter if description is a format String.this object.public ThrowableAssertAlternative<ACTUAL> describedAs(Description description)
You must set it before calling the assertion otherwise it is ignored as the failing assertion breaks the chained call by throwing an AssertionError.
This overloaded version of "describedAs" offers more flexibility than the one taking a String by allowing
users to pass their own implementation of a description. For example, a description that creates its value lazily,
only when an assertion failure occurs.
describedAs in interface Descriptable<ThrowableAssertAlternative<ACTUAL extends Throwable>>describedAs in class AbstractAssert<ThrowableAssertAlternative<ACTUAL extends Throwable>,ACTUAL extends Throwable>description - the new description to set.this object.public ThrowableAssertAlternative<?> havingCause()
Throwable has a cause and returns a new assertion object where the
cause becomes the actual Throwable in order to further assert properties of the cause ThrowableAssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no cause.public ThrowableAssertAlternative<?> havingRootCause()
Throwable has a root cause and returns a new assertion object where the
root cause becomes the actual Throwable in order to further assert properties of the cause ThrowableAssertionError - if the actual Throwable is null.AssertionError - if the actual Throwable has no root cause.Copyright © 2025. All rights reserved.