Top Description Constructors Methods
org.assertj.core.api

public final Class Fail

extends Object
Class Inheritance
Imports
org.assertj.core.internal.Failures, org.assertj.core.util.CanIgnoreReturnValue

Common failures.
Authors
Alex Ruiz, Yvonne Wang, Joel Costigliola

Constructor Summary

AccessConstructor and Description
private
Fail()

Since all its methods are static and the class is final, there is no point on creating a new instance of it.

Method Summary

Modifier and TypeMethod and Description
public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("boom")));.
fail
(String
error message.
failureMessage
)

Throws an AssertionError with the given message.

public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail()));.
fail
()

Throws an AssertionError with an empty message to be used in code like:

 doSomething(optional.orElseGet(() -> fail()));
public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("b%s", ""oom)));.
fail
(String
error message.
failureMessage
,
Object...
Arguments referenced by the format specifiers in the format string.
args
)

Throws an AssertionError with the given message built as String#format(String, Object...).

public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("boom", cause)));.
fail
(String
the description of the failed assertion. It can be null.
failureMessage
,
Throwable
cause of the error.
realCause
)

Throws an AssertionError with the given message and with the Throwable that caused the failure.

public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail(cause)));.
fail
(Throwable
cause of the error.
realCause
)

Throws an AssertionError with the Throwable that caused the failure.

public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> failBecauseExceptionWasNotThrown(IOException.class)));.
failBecauseExceptionWasNotThrown
(Class<? extends Throwable>
the Throwable class that was expected to be thrown.
throwableClass
)

Throws an AssertionError with a message explaining that a Throwable of given class was expected to be thrown but had not been.

public static void
setRemoveAssertJRelatedElementsFromStackTrace(boolean
flag.
removeAssertJRelatedElementsFromStackTrace
)

Sets whether we remove elements related to AssertJ from assertion error stack trace.

public static <
dummy return value type
T
>
T

Returns:

nothing, it's just to be used in doSomething(optional.orElseGet(() -> shouldHaveThrown(IOException.class)));.
shouldHaveThrown
(Class<? extends Throwable>
the Throwable class that was expected to be thrown.
throwableClass
)

Throws an AssertionError with a message explaining that a Throwable of given class was expected to be thrown but had not been.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Failback to summary
private Fail()

Since all its methods are static and the class is final, there is no point on creating a new instance of it.

Method Detail

failback to summary
public static <T> T fail(String failureMessage)

Throws an AssertionError with the given message.

Parameters
<T>
dummy return value type
failureMessage:String

error message.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("boom")));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with the given message.
failback to summary
public static <T> T fail()

Throws an AssertionError with an empty message to be used in code like:

 doSomething(optional.orElseGet(() -> fail()));
Parameters
<T>
dummy return value type
Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail()));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with an empty message.
Since
3.26.0
failback to summary
public static <T> T fail(String failureMessage, Object... args)

Throws an AssertionError with the given message built as String#format(String, Object...).

Parameters
<T>
dummy return value type
failureMessage:String

error message.

args:Object[]

Arguments referenced by the format specifiers in the format string.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("b%s", ""oom)));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with the given built message.
failback to summary
public static <T> T fail(String failureMessage, Throwable realCause)

Throws an AssertionError with the given message and with the Throwable that caused the failure.

Parameters
<T>
dummy return value type
failureMessage:String

the description of the failed assertion. It can be null.

realCause:Throwable

cause of the error.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail("boom", cause)));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with the given message and with the Throwable that caused the failure.
failback to summary
public static <T> T fail(Throwable realCause)

Throws an AssertionError with the Throwable that caused the failure.

Parameters
<T>
dummy return value type
realCause:Throwable

cause of the error.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> fail(cause)));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with the Throwable that caused the failure.
failBecauseExceptionWasNotThrownback to summary
public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass)

Throws an AssertionError with a message explaining that a Throwable of given class was expected to be thrown but had not been.

Parameters
<T>
dummy return value type
throwableClass:Class<? extends Throwable>

the Throwable class that was expected to be thrown.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> failBecauseExceptionWasNotThrown(IOException.class)));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with a message explaining that a Throwable of given class was expected to be thrown but had not been. Fail#shouldHaveThrown(Class) can be used as a replacement.
setRemoveAssertJRelatedElementsFromStackTraceback to summary
public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace)

Sets whether we remove elements related to AssertJ from assertion error stack trace.

Parameters
removeAssertJRelatedElementsFromStackTrace:boolean

flag.

shouldHaveThrownback to summary
public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass)

Throws an AssertionError with a message explaining that a Throwable of given class was expected to be thrown but had not been.

Parameters
<T>
dummy return value type
throwableClass:Class<? extends Throwable>

the Throwable class that was expected to be thrown.

Returns:T

nothing, it's just to be used in doSomething(optional.orElseGet(() -> shouldHaveThrown(IOException.class)));.

Annotations
@CanIgnoreReturnValue
Exceptions
AssertionError:
with a message explaining that a Throwable of given class was expected to be thrown but had not been.