Top Description Fields Constructors Methods
org.assertj.core.api

public Class ThrowableAssertAlternative<ACTUAL extends Throwable>

extends AbstractObjectAssert<ThrowableAssertAlternative<ACTUAL extends Throwable>, ACTUAL extends Throwable>
Class Inheritance
Known Direct Subclasses
org.assertj.core.api.SoftThrowableAssertAlternative
Imports
java.util.IllegalFormatException, org.assertj.core.description.Description, org.assertj.core.util.CheckReturnValue

Assertion methods for java.lang.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.

Field Summary

Modifier and TypeField and Description
private final ThrowableAssert<ACTUAL>

Constructor Summary

AccessConstructor and Description
public
ThrowableAssertAlternative(final ACTUAL actual)

Method Summary

Modifier and TypeMethod and Description
public ThrowableAssertAlternative<ACTUAL>
as(Description
the new description to set.
description
)

Overrides org.assertj.core.api.AbstractObjectAssert.as.

Overrides default org.assertj.core.api.Descriptable.as.

Sets the description of the assertion that is going to be called after.
public ThrowableAssertAlternative<ACTUAL>
describedAs(String
the new description to set.
description
,
Object...
optional parameter if description is a format String.
args
)

Overrides default org.assertj.core.api.Descriptable.describedAs.

Sets the description of the assertion that is going to be called after.
public ThrowableAssertAlternative<ACTUAL>
describedAs(Description
the new description to set.
description
)

Overrides org.assertj.core.api.AbstractAssert.describedAs.

Implements org.assertj.core.api.Descriptable.describedAs.

Sets the description of the assertion that is going to be called after.
protected ThrowableAssert<ACTUAL>
public ThrowableAssertAlternative<?>

Returns:

a new assertion object with the cause of the current actual becoming the new actual
havingCause
()

Checks if the actual 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 Throwable

public ThrowableAssertAlternative<?>

Returns:

a new assertion object with the root cause of the current actual becoming the new actual
havingRootCause
()

Checks if the actual 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 Throwable

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withCause
(Throwable
the expected cause.
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).

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withCauseExactlyInstanceOf
(Class<? extends Throwable>
the expected cause type.
type
)

Verifies that the cause of the actual Throwable is exactly an instance of the given type.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withCauseInstanceOf
(Class<? extends Throwable>
the expected cause type.
type
)

Verifies that the cause of the actual Throwable is an instance of the given type.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessage
(String
the expected message.
message
)

Verifies that the message of the actual Throwable is equal to the given one.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessage
(String
a format string representing the expected message
message
,
Object...
argument referenced by the format specifiers in the format string
parameters
)

Verifies that the message of the actual Throwable is equal to the given one built using String#format(String, Object...) syntax.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageContaining
(String
the description expected to be contained in the actual Throwable's message.
description
)

Verifies that the message of the actual Throwable contains the given description.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageContaining
(String
the description expected to be contained in the actual Throwable's message.
description
,
Object...
argument referenced by the format specifiers in the format string
parameters
)

Verifies that the message of the actual Throwable contains the given description, after being formatted using the String#format method.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageContainingAll
(CharSequence...
the Strings expected to be contained in the actual Throwable's message.
values
)

Verifies that the message of the actual Throwable contains all the given values.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageEndingWith
(String
the description expected to end the actual Throwable's message.
description
)

Verifies that the message of the actual Throwable ends with the given description.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageEndingWith
(String
the description expected to end the actual Throwable's message.
description
,
Object...
argument referenced by the format specifiers in the format string
parameters
)

Verifies that the message of the actual Throwable ends with the given description, after being formatted using the String#format method.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageMatching
(String
the regular expression of value expected to be matched the actual Throwable's message.
regex
)

Verifies that the message of the actual Throwable matches with the given regular expression.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object
withMessageNotContaining
(String
the content expected to not be contained in the actual Throwable's message.
content
)

Verifies that the message of the actual Throwable does not contain the given content or is null.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object
withMessageNotContainingAny
(CharSequence...
the contents expected to not be contained in the actual Throwable's message.
values
)

Verifies that the message of the actual Throwable does not contain any of the given values or is null.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageStartingWith
(String
the description expected to start the actual Throwable's message.
description
)

Verifies that the message of the actual Throwable starts with the given description.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withMessageStartingWith
(String
the description expected to start the actual Throwable's message.
description
,
Object...
argument referenced by the format specifiers in the format string
parameters
)

Verifies that the message of the actual Throwable starts with the given description, after being formatted using the String#format method.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withNoCause
()

Verifies that the actual Throwable does not have a cause.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withRootCauseExactlyInstanceOf
(Class<? extends Throwable>
the expected cause type.
type
)

Verifies that the root cause of the actual Throwable is exactly an instance of the given type.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withRootCauseInstanceOf
(Class<? extends Throwable>
the expected cause type.
type
)

Verifies that the root cause of the actual Throwable is an instance of the given type.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withStackTraceContaining
(String
the description expected to be contained in the actual Throwable's stack trace.
description
)

Verifies that the stack trace of the actual Throwable contains with the given description.

public ThrowableAssertAlternative<ACTUAL>

Returns:

this assertion object.
withStackTraceContaining
(String
the description expected to be contained in the actual Throwable's stack trace.
description
,
Object...
argument referenced by the format specifiers in the format string
parameters
)

Verifies that the stack trace of the actual Throwable contains with the given description, after being formatted using the String#format method.

Inherited from org.assertj.core.api.AbstractObjectAssert:
asdoesNotReturnextractingextractingextractingextractingextractingextractingextractingForProxygetComparatorsByTypehasAllNullFieldsOrPropertieshasAllNullFieldsOrPropertiesExcepthasFieldOrPropertyhasFieldOrPropertyWithValuehasNoNullFieldsOrPropertieshasNoNullFieldsOrPropertiesExcepthasOnlyFieldsisEqualToComparingFieldByFieldisEqualToComparingFieldByFieldRecursivelyisEqualToComparingOnlyGivenFieldsisEqualToIgnoringGivenFieldsisEqualToIgnoringNullFieldsnewObjectAssertreturnsusingComparatorForFieldsusingComparatorForTypeusingRecursiveAssertionusingRecursiveAssertionusingRecursiveComparisonusingRecursiveComparisonwithAssertionStatewithComparatorByPropertyOrFieldwithTypeComparator

Field Detail

delegateback to summary
private final ThrowableAssert<ACTUAL> delegate

Constructor Detail

ThrowableAssertAlternativeback to summary
public ThrowableAssertAlternative(final ACTUAL actual)

Method Detail

asback to summary
public ThrowableAssertAlternative<ACTUAL> as(Description description)

Overrides org.assertj.core.api.AbstractObjectAssert.as.

Overrides default org.assertj.core.api.Descriptable.as.

Doc from org.assertj.core.api.Descriptable.as.

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 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.

Parameters
description:Description

the new description to set.

Returns:ThrowableAssertAlternative<ACTUAL>

this object.

Annotations
@Override
describedAsback to summary
public ThrowableAssertAlternative<ACTUAL> describedAs(String description, Object... args)

Overrides default org.assertj.core.api.Descriptable.describedAs.

Doc from org.assertj.core.api.Descriptable.describedAs.

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 as(String, Object...) since "as" is a keyword in Groovy.

Parameters
description:String

the new description to set.

args:Object[]

optional parameter if description is a format String.

Returns:ThrowableAssertAlternative<ACTUAL>

this object.

Annotations
@Override
@CheckReturnValue
describedAsback to summary
public ThrowableAssertAlternative<ACTUAL> describedAs(Description description)

Overrides org.assertj.core.api.AbstractAssert.describedAs.

Implements org.assertj.core.api.Descriptable.describedAs.

Doc from org.assertj.core.api.AbstractAssert.describedAs.

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 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.

Parameters
description:Description

Doc from org.assertj.core.api.Descriptable.describedAs.

the new description to set.

Returns:ThrowableAssertAlternative<ACTUAL>

Doc from org.assertj.core.api.Descriptable.describedAs.

this object.

Annotations
@Override
@CheckReturnValue
getDelegateback to summary
protected ThrowableAssert<ACTUAL> getDelegate()
havingCauseback to summary
public ThrowableAssertAlternative<?> havingCause()

Checks if the actual 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 Throwable

Returns:ThrowableAssertAlternative<?>

a new assertion object with the cause of the current actual becoming the new actual

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no cause.
Since
3.16.0
havingRootCauseback to summary
public ThrowableAssertAlternative<?> havingRootCause()

Checks if the actual 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 Throwable

Returns:ThrowableAssertAlternative<?>

a new assertion object with the root cause of the current actual becoming the new actual

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no root cause.
Since
3.16.0
withCauseback to summary
public 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).

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:Throwable

the expected cause.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has not the given cause.
See Also
AbstractThrowableAssert#hasCause(Throwable)
withCauseExactlyInstanceOfback to summary
public ThrowableAssertAlternative<ACTUAL> withCauseExactlyInstanceOf(Class<? extends Throwable> type)

Verifies that the cause of the actual 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);
Parameters
type:Class<? extends Throwable>

the expected cause type.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
NullPointerException:
if given type is null.
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no cause.
  • if the cause of the actual Throwable is not exactly an instance of the given type.
See Also
AbstractThrowableAssert#hasCauseExactlyInstanceOf(Class)
withCauseInstanceOfback to summary
public ThrowableAssertAlternative<ACTUAL> withCauseInstanceOf(Class<? extends Throwable> type)

Verifies that the cause of the actual 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);
Parameters
type:Class<? extends Throwable>

the expected cause type.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
NullPointerException:
if given type is null.
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no cause.
  • if the cause of the actual Throwable is not an instance of the given type.
See Also
AbstractThrowableAssert#hasCauseInstanceOf(Class)
withMessageback to summary
public ThrowableAssertAlternative<ACTUAL> withMessage(String message)

Verifies that the message of the actual 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");
Parameters
message:String

the expected message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable is not equal to the given one.
See Also
AbstractThrowableAssert#hasMessage(String)
withMessageback to summary
public 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.

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");
Parameters
message:String

a format string representing the expected message

parameters:Object[]

argument referenced by the format specifiers in the format string

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable is not equal to the given one.
See Also
AbstractThrowableAssert#hasMessage(String)
withMessageContainingback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageContaining(String description)

Verifies that the message of the actual 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");
Parameters
description:String

the description expected to be contained in the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable does not contain the given description.
See Also
AbstractThrowableAssert#hasMessageContaining(String)
withMessageContainingback to summary
public 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 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);
Parameters
description:String

the description expected to be contained in the actual Throwable's message.

parameters:Object[]

argument referenced by the format specifiers in the format string

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • 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...).
See Also
AbstractThrowableAssert#hasMessageContaining(String, Object...)
withMessageContainingAllback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageContainingAll(CharSequence... values)

Verifies that the message of the actual 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");
Parameters
values:CharSequence[]

the Strings expected to be contained in the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable does not contain all the given values.
See Also
AbstractThrowableAssert#hasMessageContainingAll(CharSequence...)
withMessageEndingWithback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageEndingWith(String description)

Verifies that the message of the actual 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");
Parameters
description:String

the description expected to end the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable does not end with the given description.
See Also
AbstractThrowableAssert#hasMessageEndingWith(String)
withMessageEndingWithback to summary
public 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 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);
Parameters
description:String

the description expected to end the actual Throwable's message.

parameters:Object[]

argument referenced by the format specifiers in the format string

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • 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...).
See Also
AbstractThrowableAssert#hasMessageEndingWith(String, Object...)
withMessageMatchingback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageMatching(String regex)

Verifies that the message of the actual 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");
Parameters
regex:String

the regular expression of value expected to be matched the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable does not match the given regular expression.
NullPointerException:
if the regex is null
See Also
AbstractThrowableAssert#hasMessageMatching(String)
withMessageNotContainingback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageNotContaining(String content)

Verifies that the message of the actual 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");
Parameters
content:String

the content expected to not be contained in the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable contains the given content.
See Also
AbstractThrowableAssert#hasMessageNotContaining(String)
withMessageNotContainingAnyback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageNotContainingAny(CharSequence... values)

Verifies that the message of the actual 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");
Parameters
values:CharSequence[]

the contents expected to not be contained in the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable contains any of the given values.
See Also
AbstractThrowableAssert#hasMessageNotContainingAny(CharSequence...)
withMessageStartingWithback to summary
public ThrowableAssertAlternative<ACTUAL> withMessageStartingWith(String description)

Verifies that the message of the actual 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");
Parameters
description:String

the description expected to start the actual Throwable's message.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the message of the actual Throwable does not start with the given description.
See Also
AbstractThrowableAssert#hasMessageStartingWith(String)
withMessageStartingWithback to summary
public 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 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");
Parameters
description:String

the description expected to start the actual Throwable's message.

parameters:Object[]

argument referenced by the format specifiers in the format string

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • 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...).
See Also
AbstractThrowableAssert#hasMessageStartingWith(String, Object...)
withNoCauseback to summary
public ThrowableAssertAlternative<ACTUAL> withNoCause()

Verifies that the actual 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();
Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has a cause.
See Also
AbstractThrowableAssert#hasNoCause()
withRootCauseExactlyInstanceOfback to summary
public ThrowableAssertAlternative<ACTUAL> withRootCauseExactlyInstanceOf(Class<? extends Throwable> type)

Verifies that the root cause of the actual 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);
Parameters
type:Class<? extends Throwable>

the expected cause type.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
NullPointerException:
if given type is null.
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no cause.
  • if the root cause of the actual Throwable is not exactly an instance of the given type.
See Also
AbstractThrowableAssert#hasRootCauseExactlyInstanceOf(Class)
withRootCauseInstanceOfback to summary
public ThrowableAssertAlternative<ACTUAL> withRootCauseInstanceOf(Class<? extends Throwable> type)

Verifies that the root cause of the actual 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);
Parameters
type:Class<? extends Throwable>

the expected cause type.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
NullPointerException:
if given type is null.
AssertionError:
  • if the actual Throwable is null.
  • if the actual Throwable has no cause.
  • if the cause of the actual Throwable is not an instance of the given type.
See Also
AbstractThrowableAssert#hasRootCauseInstanceOf(Class)
withStackTraceContainingback to summary
public ThrowableAssertAlternative<ACTUAL> withStackTraceContaining(String description)

Verifies that the stack trace of the actual 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");
Parameters
description:String

the description expected to be contained in the actual Throwable's stack trace.

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • if the stack trace of the actual Throwable does not contain the given description.
See Also
AbstractThrowableAssert#hasStackTraceContaining(String)
withStackTraceContainingback to summary
public 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 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);
Parameters
description:String

the description expected to be contained in the actual Throwable's stack trace.

parameters:Object[]

argument referenced by the format specifiers in the format string

Returns:ThrowableAssertAlternative<ACTUAL>

this assertion object.

Exceptions
AssertionError:
  • if the actual Throwable is null.
  • 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...).
See Also
AbstractThrowableAssert#hasStackTraceContaining(String, Object...)