Class ThrowableAssertAlternative<T extends Throwable>
- java.lang.Object
-
- org.assertj.core.api.AbstractAssert<ThrowableAssertAlternative<T>,T>
-
- org.assertj.core.api.ThrowableAssertAlternative<T>
-
- All Implemented Interfaces:
Assert<ThrowableAssertAlternative<T>,T>,Descriptable<ThrowableAssertAlternative<T>>,ExtensionPoints<ThrowableAssertAlternative<T>,T>
public class ThrowableAssertAlternative<T extends Throwable> extends AbstractAssert<ThrowableAssertAlternative<T>,T>
Assertion methods forThrowablesimilar toThrowableAssertbut with assertions methods named differently to make testing code fluent (ex :withMessageinstead ofhasMessage.
This class is linked with theassertThatExceptionOfType(IOException.class) .isThrownBy(() -> { throw new IOException("boom! tcha!"); }); .withMessage("boom! %s", "tcha!");ThrowableTypeAssertand allow to check that an exception type is thrown by a lambda.
-
-
Field Summary
Fields Modifier and Type Field Description private ThrowableAssertdelegate-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, assertionErrorCreator, conditions, info, myself, objects, throwUnsupportedExceptionOnEquals
-
-
Constructor Summary
Constructors Constructor Description ThrowableAssertAlternative(T actual)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ThrowableAssertAlternative<T>describedAs(String description, Object... args)Sets the description of the assertion that is going to be called after.ThrowableAssertAlternative<T>describedAs(Description description)Sets the description of the assertion that is going to be called after.ThrowableAssertAlternative<T>withCause(Throwable cause)Verifies that the actualThrowablehas a cause similar to the given one, that is with same type and message (it does not useequalsmethod for comparison).ThrowableAssertAlternative<T>withCauseExactlyInstanceOf(Class<? extends Throwable> type)Verifies that the cause of the actualThrowableis exactly an instance of the given type.ThrowableAssertAlternative<T>withCauseInstanceOf(Class<? extends Throwable> type)Verifies that the cause of the actualThrowableis an instance of the given type.ThrowableAssertAlternative<T>withMessage(String message)Verifies that the message of the actualThrowableis equal to the given one.ThrowableAssertAlternative<T>withMessage(String message, Object... parameters)Verifies that the message of the actualThrowableis equal to the given one built usingString.format(String, Object...)syntax.ThrowableAssertAlternative<T>withMessageContaining(String description)Verifies that the message of the actualThrowablecontains the given description.ThrowableAssertAlternative<T>withMessageContainingAll(CharSequence... values)Verifies that the message of the actualThrowablecontains all the given values.ThrowableAssertAlternative<T>withMessageEndingWith(String description)Verifies that the message of the actualThrowableends with the given description.ThrowableAssertAlternative<T>withMessageMatching(String regex)Verifies that the message of the actualThrowablematches with the given regular expression.ThrowableAssertAlternative<T>withMessageNotContaining(String content)Verifies that the message of the actualThrowabledoes not contain the given content or is null.ThrowableAssertAlternative<T>withMessageNotContainingAny(CharSequence... values)Verifies that the message of the actualThrowabledoes not contain any of the given values or isnull.ThrowableAssertAlternative<T>withMessageStartingWith(String description)Verifies that the message of the actualThrowablestarts with the given description.ThrowableAssertAlternative<T>withNoCause()Verifies that the actualThrowabledoes not have a cause.ThrowableAssertAlternative<T>withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)Verifies that the root cause of the actualThrowableis exactly an instance of the given type.ThrowableAssertAlternative<T>withRootCauseInstanceOf(Class<? extends Throwable> type)Verifies that the root cause of the actualThrowableis an instance of the given type.ThrowableAssertAlternative<T>withStackTraceContaining(String description)Verifies that the stack trace of the actualThrowablecontains with the given description.-
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, asInstanceOf, asList, asString, 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, withAssertionState, withFailMessage, withRepresentation, withThreadDumpOnError
-
-
-
-
Field Detail
-
delegate
private ThrowableAssert delegate
-
-
Constructor Detail
-
ThrowableAssertAlternative
public ThrowableAssertAlternative(T actual)
-
-
Method Detail
-
withMessage
public ThrowableAssertAlternative<T> withMessage(String message)
Verifies that the message of the actualThrowableis 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");- Parameters:
message- the expected message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowableis not equal to the given one.- See Also:
AbstractThrowableAssert.hasMessage(String)
-
withMessage
public ThrowableAssertAlternative<T> withMessage(String message, Object... parameters)
Verifies that the message of the actualThrowableis equal to the given one built usingString.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");- 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 actualThrowableisnull.AssertionError- if the message of the actualThrowableis not equal to the given one.- See Also:
AbstractThrowableAssert.hasMessage(String)
-
withCause
public ThrowableAssertAlternative<T> withCause(Throwable cause)
Verifies that the actualThrowablehas a cause similar to the given one, that is with same type and message (it does not useequalsmethod 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);- Parameters:
cause- the expected cause.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas not the given cause.- See Also:
AbstractThrowableAssert.hasCause(Throwable)
-
withNoCause
public ThrowableAssertAlternative<T> withNoCause()
Verifies that the actualThrowabledoes 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();- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas a cause.- See Also:
AbstractThrowableAssert.hasNoCause()
-
withMessageStartingWith
public ThrowableAssertAlternative<T> withMessageStartingWith(String description)
Verifies that the message of the actualThrowablestarts 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");- Parameters:
description- the description expected to start the actualThrowable's message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowabledoes not start with the given description.- See Also:
AbstractThrowableAssert.hasMessageStartingWith(String)
-
withMessageContaining
public ThrowableAssertAlternative<T> withMessageContaining(String description)
Verifies that the message of the actualThrowablecontains 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");- Parameters:
description- the description expected to be contained in the actualThrowable's message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowabledoes not contain the given description.- See Also:
AbstractThrowableAssert.hasMessageContaining(String)
-
withMessageContainingAll
public ThrowableAssertAlternative<T> withMessageContainingAll(CharSequence... values)
Verifies that the message of the actualThrowablecontains all the given values.Examples:
Throwable illegalArgumentException = new IllegalArgumentException("wrong amount 123"); // assertion will pass assertThatExceptionOfType(Throwable.class) .isThrownBy(() -> {throw illegalArgumentException;}) .withMessageContainingAll("amount", "123"); // assertion will fail assertThatExceptionOfType(Throwable.class) .isThrownBy(() -> {throw illegalArgumentException;}) .withMessageContainingAll("456");- Parameters:
values- the Strings expected to be contained in the actualThrowable's message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowabledoes not contain all the given values.- See Also:
AbstractThrowableAssert.hasMessageContainingAll(CharSequence...)
-
withMessageNotContaining
public ThrowableAssertAlternative<T> withMessageNotContaining(String content)
Verifies that the message of the actualThrowabledoes 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 will fail assertThatExceptionOfType(Exception.class) .isThrownBy(codeThrowing(new Exception("boom"))) .withMessageNotContaining("boom");- Parameters:
content- the content exptected to not be contained in the actualThrowables's message.- Returns:
- this assertion object
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowablecontains the given content.- See Also:
AbstractThrowableAssert.hasMessageNotContaining(String)
-
withMessageNotContainingAny
public ThrowableAssertAlternative<T> withMessageNotContainingAny(CharSequence... values)
Verifies that the message of the actualThrowabledoes not contain any of the given values or isnull.Examples:
//assertions will pass assertThatExceptionOfType(Exception.class) .isThrownBy(codeThrowing(new Exception("boom"))) .withMessageNotContainingAny("bam"); assertThatExceptionOfType(Exception.class) .isThrownBy(codeThrowing(new Exception())) .withMessageNotContainingAny("bam"); // assertion will fail assertThatExceptionOfType(Exception.class) .isThrownBy(codeThrowing(new Exception("boom"))) .withMessageNotContainingAny("bam", "boom");- Parameters:
values- the contents expected to not be contained in the actualThrowables's message.- Returns:
- this assertion object
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowablecontains any of the given values.- See Also:
AbstractThrowableAssert.hasMessageNotContainingAny(CharSequence...)
-
withStackTraceContaining
public ThrowableAssertAlternative<T> withStackTraceContaining(String description)
Verifies that the stack trace of the actualThrowablecontains 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");- Parameters:
description- the description expected to be contained in the actualThrowable's stack trace.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the stack trace of the actualThrowabledoes not contain the given description.- See Also:
AbstractThrowableAssert.hasStackTraceContaining(String)
-
withMessageMatching
public ThrowableAssertAlternative<T> withMessageMatching(String regex)
Verifies that the message of the actualThrowablematches 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");- Parameters:
regex- the regular expression of value expected to be matched the actualThrowable's message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowabledoes not match the given regular expression.NullPointerException- if the regex is null- See Also:
AbstractThrowableAssert.hasMessageMatching(String)
-
withMessageEndingWith
public ThrowableAssertAlternative<T> withMessageEndingWith(String description)
Verifies that the message of the actualThrowableends 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");- Parameters:
description- the description expected to end the actualThrowable's message.- Returns:
- this assertion object.
- Throws:
AssertionError- if the actualThrowableisnull.AssertionError- if the message of the actualThrowabledoes not end with the given description.- See Also:
AbstractThrowableAssert.hasMessageEndingWith(String)
-
withCauseInstanceOf
public ThrowableAssertAlternative<T> withCauseInstanceOf(Class<? extends Throwable> type)
Verifies that the cause of the actualThrowableis 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);- Parameters:
type- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException- if given type isnull.AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas no cause.AssertionError- if the cause of the actualThrowableis not an instance of the given type.- See Also:
AbstractThrowableAssert.hasCauseInstanceOf(Class)
-
withCauseExactlyInstanceOf
public ThrowableAssertAlternative<T> withCauseExactlyInstanceOf(Class<? extends Throwable> type)
Verifies that the cause of the actualThrowableis 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);- Parameters:
type- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException- if given type isnull.AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas no cause.AssertionError- if the cause of the actualThrowableis not exactly an instance of the given type.- See Also:
AbstractThrowableAssert.hasCauseExactlyInstanceOf(Class)
-
withRootCauseInstanceOf
public ThrowableAssertAlternative<T> withRootCauseInstanceOf(Class<? extends Throwable> type)
Verifies that the root cause of the actualThrowableis 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);- Parameters:
type- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException- if given type isnull.AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas no cause.AssertionError- if the cause of the actualThrowableis not an instance of the given type.- See Also:
AbstractThrowableAssert.hasRootCauseInstanceOf(Class)
-
withRootCauseExactlyInstanceOf
public ThrowableAssertAlternative<T> withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)
Verifies that the root cause of the actualThrowableis 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);- Parameters:
type- the expected cause type.- Returns:
- this assertion object.
- Throws:
NullPointerException- if given type isnull.AssertionError- if the actualThrowableisnull.AssertionError- if the actualThrowablehas no cause.AssertionError- if the root cause of the actualThrowableis not exactly an instance of the given type.- See Also:
AbstractThrowableAssert.hasRootCauseExactlyInstanceOf(Class)
-
describedAs
public ThrowableAssertAlternative<T> describedAs(String description, Object... args)
Sets the description of the assertion that is going to be called after.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...)- Specified by:
describedAsin interfaceDescriptable<T extends Throwable>- Overrides:
describedAsin classAbstractAssert<ThrowableAssertAlternative<T extends Throwable>,T extends Throwable>- Parameters:
description- the new description to set.args- optional parameter if description is a format String.- Returns:
thisobject.
-
describedAs
public ThrowableAssertAlternative<T> describedAs(Description description)
Sets the description of the assertion that is going to be called after.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
Stringby 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.- Specified by:
describedAsin interfaceDescriptable<T extends Throwable>- Overrides:
describedAsin classAbstractAssert<ThrowableAssertAlternative<T extends Throwable>,T extends Throwable>- Parameters:
description- the new description to set.- Returns:
thisobject.
-
-