Class ThrowableTypeAssert<T extends java.lang.Throwable>
- java.lang.Object
-
- org.assertj.core.api.ThrowableTypeAssert<T>
-
- Type Parameters:
T
- type of throwable to be thrown.
- All Implemented Interfaces:
Descriptable<ThrowableTypeAssert<T>>
public class ThrowableTypeAssert<T extends java.lang.Throwable> extends java.lang.Object implements Descriptable<ThrowableTypeAssert<T>>
Assertion class checkingThrowable
type.The class itself does not do much, it delegates the work to
ThrowableAssertAlternative
after callingisThrownBy(ThrowableAssert.ThrowingCallable)
.
-
-
Field Summary
Fields Modifier and Type Field Description protected Description
description
protected java.lang.Class<? extends T>
expectedThrowableType
-
Constructor Summary
Constructors Constructor Description ThrowableTypeAssert(java.lang.Class<? extends T> throwableType)
Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ThrowableTypeAssert<T>
as(java.lang.String description, java.lang.Object... args)
Sets the description of the assertion that is going to be called after.ThrowableTypeAssert<T>
as(Description description)
Sets the description of the assertion that is going to be called after.ThrowableTypeAssert<T>
describedAs(java.lang.String description, java.lang.Object... args)
Sets the description of the assertion that is going to be called after.ThrowableTypeAssert<T>
describedAs(Description description)
Sets the description of the assertion that is going to be called after.ThrowableAssertAlternative<T>
isThrownBy(ThrowableAssert.ThrowingCallable throwingCallable)
Assert that an exception of type T is thrown by thethrowingCallable
and allow to chain assertions on the thrown exception.
-
-
-
Field Detail
-
expectedThrowableType
protected final java.lang.Class<? extends T extends java.lang.Throwable> expectedThrowableType
-
description
protected Description description
-
-
Constructor Detail
-
ThrowableTypeAssert
public ThrowableTypeAssert(java.lang.Class<? extends T> throwableType)
Default constructor.- Parameters:
throwableType
- class representing the target (expected) exception.
-
-
Method Detail
-
isThrownBy
public ThrowableAssertAlternative<T> isThrownBy(ThrowableAssert.ThrowingCallable throwingCallable)
Assert that an exception of type T is thrown by thethrowingCallable
and allow to chain assertions on the thrown exception.Example:
assertThatExceptionOfType(IOException.class).isThrownBy(() -> { throw new IOException("boom!"); }) .withMessage("boom!");
- Parameters:
throwingCallable
- code throwing the exception of expected type- Returns:
- return a
ThrowableAssertAlternative
.
-
as
public ThrowableTypeAssert<T> as(java.lang.String description, java.lang.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.
The description follows
String.format(String, Object...)
syntax.Example :
try { // set a bad age to Mr Frodo which is really 33 years old. frodo.setAge(50); // specify a test description (call as() before the assertion !), it supports String format syntax. assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33); } catch (AssertionError e) { assertThat(e).hasMessage("[check Frodo's age] expected:<[33]> but was:<[50]>"); }
- Specified by:
as
in interfaceDescriptable<T extends java.lang.Throwable>
- Parameters:
description
- the new description to set.args
- optional parameter if description is a format String.- Returns:
this
object.- See Also:
Descriptable.describedAs(String, Object...)
-
as
public ThrowableTypeAssert<T> as(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
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.- Specified by:
as
in interfaceDescriptable<T extends java.lang.Throwable>
- Parameters:
description
- the new description to set.- Returns:
this
object.- See Also:
Descriptable.describedAs(Description)
-
describedAs
public ThrowableTypeAssert<T> describedAs(java.lang.String description, java.lang.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:
describedAs
in interfaceDescriptable<T extends java.lang.Throwable>
- Parameters:
description
- the new description to set.args
- optional parameter if description is a format String.- Returns:
this
object.
-
describedAs
public ThrowableTypeAssert<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
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.- Specified by:
describedAs
in interfaceDescriptable<T extends java.lang.Throwable>
- Parameters:
description
- the new description to set.- Returns:
this
object.
-
-