Package org.assertj.core.api
Class AbstractThrowableAssert<SELF extends AbstractThrowableAssert<SELF,ACTUAL>,ACTUAL extends java.lang.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 java.lang.Throwable> extends AbstractObjectAssert<SELF,ACTUAL>
Base class for all implementations of assertions forThrowable
s.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) Throwables
throwables
-
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, conditions, info, myself, objects
-
-
Constructor Summary
Constructors Constructor Description AbstractThrowableAssert(ACTUAL actual, java.lang.Class<?> selfType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
doesNotThrowAnyException()
Verifies that theThrowableAssert.ThrowingCallable
didn't raise a throwable.protected SELF
hasBeenThrown()
SELF
hasCause(java.lang.Throwable cause)
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).SELF
hasCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actualThrowable
is exactly an instance of the given type.SELF
hasCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the cause of the actualThrowable
is an instance of the given type.SELF
hasMessage(java.lang.String message)
Verifies that the message of the actualThrowable
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 theString.format(java.lang.String, java.lang.Object...)
method.SELF
hasMessageContaining(java.lang.String description)
Verifies that the message of the actualThrowable
contains the given description.SELF
hasMessageEndingWith(java.lang.String description)
Verifies that the message of the actualThrowable
ends with the given description.SELF
hasMessageMatching(java.lang.String regex)
Verifies that the message of the actualThrowable
matches the given regular expression.SELF
hasMessageStartingWith(java.lang.String description)
Verifies that the message of the actualThrowable
starts with the given description.SELF
hasNoCause()
Verifies that the actualThrowable
does not have a cause.SELF
hasNoSuppressedExceptions()
Verifies that the actualThrowable
has no suppressed exceptions.SELF
hasRootCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
Verifies that the root cause of the actualThrowable
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 actualThrowable
is an instance of the given type.SELF
hasStackTraceContaining(java.lang.String description)
Verifies that the stack trace of the actualThrowable
contains the given description.SELF
hasSuppressedException(java.lang.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).-
Methods inherited from class org.assertj.core.api.AbstractObjectAssert
as, as, defaultTypeComparators, extracting, extracting, hasFieldOrProperty, hasFieldOrPropertyWithValue, hasNoNullFieldsOrProperties, hasNoNullFieldsOrPropertiesExcept, isEqualToComparingFieldByField, isEqualToComparingFieldByFieldRecursively, isEqualToComparingOnlyGivenFields, isEqualToIgnoringGivenFields, isEqualToIgnoringNullFields, returns, usingComparatorForFields, usingComparatorForType
-
Methods inherited from class org.assertj.core.api.AbstractAssert
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, withThreadDumpOnError
-
-
-
-
Field Detail
-
throwables
Throwables throwables
-
-
Constructor Detail
-
AbstractThrowableAssert
public AbstractThrowableAssert(ACTUAL actual, java.lang.Class<?> selfType)
-
-
Method Detail
-
hasBeenThrown
protected SELF hasBeenThrown()
-
hasMessage
public SELF hasMessage(java.lang.String message)
Verifies that the message of the actualThrowable
is equal to the given one.- Parameters:
message
- the expected message.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
is not equal to the given one.
-
hasMessage
public 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 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:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
is not equal to the given one.java.util.IllegalFormatException
- if the message contains an illegal syntax according toString.format(String, Object...)
.
-
hasCause
public SELF hasCause(java.lang.Throwable cause)
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()
- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has not the given cause.
-
hasNoCause
public SELF hasNoCause()
Verifies that the actualThrowable
does not have a cause.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has a cause.
-
hasMessageStartingWith
public SELF hasMessageStartingWith(java.lang.String description)
Verifies that the message of the actualThrowable
starts with the given description.- Parameters:
description
- the description expected to start the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
does not start with the given description.
-
hasMessageContaining
public SELF hasMessageContaining(java.lang.String description)
Verifies that the message of the actualThrowable
contains the given description.- Parameters:
description
- the description expected to be contained in the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
does not contain the given description.
-
hasStackTraceContaining
public SELF hasStackTraceContaining(java.lang.String description)
Verifies that the stack trace of the actualThrowable
contains the given description.- Parameters:
description
- the description expected to be contained in the actualThrowable
's stack trace.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the stack trace of the actualThrowable
does not contain the given description.
-
hasMessageMatching
public SELF hasMessageMatching(java.lang.String regex)
Verifies that the message of the actualThrowable
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");
- Parameters:
regex
- the regular expression of value expected to be matched the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
does not match the given regular expression.java.lang.NullPointerException
- if the regex is null
-
hasMessageEndingWith
public SELF hasMessageEndingWith(java.lang.String description)
Verifies that the message of the actualThrowable
ends with the given description.- Parameters:
description
- the description expected to end the actualThrowable
's message.- Returns:
- this assertion object.
- Throws:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the message of the actualThrowable
does not end with the given description.
-
hasCauseInstanceOf
public SELF hasCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
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:
java.lang.NullPointerException
- if given type isnull
.java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has no cause.java.lang.AssertionError
- if the cause of the actualThrowable
is not an instance of the given type.
-
hasCauseExactlyInstanceOf
public SELF hasCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
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:
java.lang.NullPointerException
- if given type isnull
.java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has no cause.java.lang.AssertionError
- if the cause of the actualThrowable
is not exactly an instance of the given type.
-
hasRootCauseInstanceOf
public SELF hasRootCauseInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
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:
java.lang.NullPointerException
- if given type isnull
.java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has no cause.java.lang.AssertionError
- if the cause of the actualThrowable
is not an instance of the given type.
-
hasRootCauseExactlyInstanceOf
public SELF hasRootCauseExactlyInstanceOf(java.lang.Class<? extends java.lang.Throwable> type)
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:
java.lang.NullPointerException
- if given type isnull
.java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.AssertionError
- if the actualThrowable
has no cause.java.lang.AssertionError
- if the root cause of the actualThrowable
is not exactly an instance of the given type.
-
hasNoSuppressedExceptions
public SELF 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:
java.lang.NullPointerException
- if given type isnull
.java.lang.AssertionError
- if the actualThrowable
has any suppressed exceptions.- Since:
- 2.6.0 / 3.6.0
-
hasSuppressedException
public SELF hasSuppressedException(java.lang.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). 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:
java.lang.AssertionError
- if the actualThrowable
isnull
.java.lang.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:
java.lang.AssertionError
- if the actual statement raised aThrowable
.- Since:
- 3.7.0
-
-