Package org.assertj.core.api
Class Fail
java.lang.Object
org.assertj.core.api.Fail
Common failures.
- Author:
- Alex Ruiz, Yvonne Wang, Joel Costigliola
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tfail()Throws anAssertionErrorwith an empty message to be used in code like:static <T> TThrows anAssertionErrorwith the given message.static <T> TThrows anAssertionErrorwith the given message built asString.format(String, Object...).static <T> TThrows anAssertionErrorwith the given message and with theThrowablethat caused the failure.static <T> TThrows anAssertionErrorwith theThrowablethat caused the failure.static <T> TfailBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) Throws anAssertionErrorwith a message explaining that aThrowableof given class was expected to be thrown but had not been.static voidsetRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace) Sets whether we remove elements related to AssertJ from assertion error stack trace.static <T> TshouldHaveThrown(Class<? extends Throwable> throwableClass) Throws anAssertionErrorwith a message explaining that aThrowableof given class was expected to be thrown but had not been.
-
Method Details
-
setRemoveAssertJRelatedElementsFromStackTrace
public static void setRemoveAssertJRelatedElementsFromStackTrace(boolean removeAssertJRelatedElementsFromStackTrace) Sets whether we remove elements related to AssertJ from assertion error stack trace.- Parameters:
removeAssertJRelatedElementsFromStackTrace- flag.
-
fail
Throws anAssertionErrorwith the given message.- Type Parameters:
T- dummy return value type- Parameters:
failureMessage- error message.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> fail("boom")));. - Throws:
AssertionError- with the given message.
-
fail
Throws anAssertionErrorwith an empty message to be used in code like:doSomething(optional.orElseGet(() -> fail()));- Type Parameters:
T- dummy return value type- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> fail()));. - Throws:
AssertionError- with an empty message.- Since:
- 3.26.0
-
fail
Throws anAssertionErrorwith the given message built asString.format(String, Object...).- Type Parameters:
T- dummy return value type- Parameters:
failureMessage- error message.args- Arguments referenced by the format specifiers in the format string.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> fail("b%s", ""oom)));. - Throws:
AssertionError- with the given built message.
-
fail
Throws anAssertionErrorwith the given message and with theThrowablethat caused the failure.- Type Parameters:
T- dummy return value type- Parameters:
failureMessage- the description of the failed assertion. It can benull.realCause- cause of the error.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> fail("boom", cause)));. - Throws:
AssertionError- with the given message and with theThrowablethat caused the failure.
-
fail
Throws anAssertionErrorwith theThrowablethat caused the failure.- Type Parameters:
T- dummy return value type- Parameters:
realCause- cause of the error.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> fail(cause)));. - Throws:
AssertionError- with theThrowablethat caused the failure.
-
failBecauseExceptionWasNotThrown
@Contract("_ -> fail") public static <T> T failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) Throws anAssertionErrorwith a message explaining that aThrowableof given class was expected to be thrown but had not been.- Type Parameters:
T- dummy return value type- Parameters:
throwableClass- the Throwable class that was expected to be thrown.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> failBecauseExceptionWasNotThrown(IOException.class)));. - Throws:
AssertionError- with a message explaining that aThrowableof given class was expected to be thrown but had not been.- See Also:
-
shouldHaveThrown
@Contract("_ -> fail") public static <T> T shouldHaveThrown(Class<? extends Throwable> throwableClass) Throws anAssertionErrorwith a message explaining that aThrowableof given class was expected to be thrown but had not been.- Type Parameters:
T- dummy return value type- Parameters:
throwableClass- the Throwable class that was expected to be thrown.- Returns:
- nothing, it's just to be used in
doSomething(optional.orElseGet(() -> shouldHaveThrown(IOException.class)));. - Throws:
AssertionError- with a message explaining that aThrowableof given class was expected to be thrown but had not been.
-