Top Description Constructors Methods
org.junit.jupiter.api

public Class Assumptions

extends Object
Class Inheritance
Annotations
@API
status:STABLE
since:5.0
Static Imports
org.apiguardian.api.API.Status.STABLE

Assumptions is a collection of utility methods that support conditional test execution based on assumptions.

In direct contrast to failed assertions, failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted. However, failed assertions and other exceptions thrown by tests take precedence over failed assumptions when both are thrown during the execution of a test (for example, by different lifecycle methods), regardless of the order they are thrown in. In such cases, the test will be reported as failed rather than aborted.

Assumptions are typically used whenever it does not make sense to continue execution of a given test method — for example, if the test depends on something that does not exist in the current runtime environment.

Although it is technically possible to extend this class, extension is strongly discouraged. The JUnit Team highly recommends that the methods defined in this class be used via static imports.

Since
5.0
See Also
TestAbortedException, Assertions

Constructor Summary

AccessConstructor and Description
protected
Assumptions()

Protected constructor allowing subclassing but not direct instantiation.

Method Summary

Modifier and TypeMethod and Description
public static <V> V
abort()

Abort the test without a message.

public static <V> V
abort(String
the message to be included in the TestAbortedException
message
)

Abort the test with the given message.

public static <V> V
abort(Supplier<String>
the supplier of the message to be included in the TestAbortedException
messageSupplier
)

Abort the test with the supplied message.

public static void
assumeFalse(boolean
the assumption to validate
assumption
)

Validate the given assumption.

public static void
assumeFalse(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
)

Validate the given assumption.

public static void
assumeFalse(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
,
@Nullable String
the message to be included in the TestAbortedException if the assumption is invalid
message
)

Validate the given assumption.

public static void
assumeFalse(boolean
the assumption to validate
assumption
,
Supplier<@Nullable String>
the supplier of the message to be included in the TestAbortedException if the assumption is invalid
messageSupplier
)

Validate the given assumption.

public static void
assumeFalse(boolean
the assumption to validate
assumption
,
@Nullable String
the message to be included in the TestAbortedException if the assumption is invalid
message
)

Validate the given assumption.

public static void
assumeFalse(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
,
Supplier<@Nullable String>
the supplier of the message to be included in the TestAbortedException if the assumption is invalid
messageSupplier
)

Validate the given assumption.

public static void
assumeTrue(boolean
the assumption to validate
assumption
)

Validate the given assumption.

public static void
assumeTrue(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
)

Validate the given assumption.

public static void
assumeTrue(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
,
@Nullable String
the message to be included in the TestAbortedException if the assumption is invalid
message
)

Validate the given assumption.

public static void
assumeTrue(boolean
the assumption to validate
assumption
,
Supplier<@Nullable String>
the supplier of the message to be included in the TestAbortedException if the assumption is invalid
messageSupplier
)

Validate the given assumption.

public static void
assumeTrue(boolean
the assumption to validate
assumption
,
@Nullable String
the message to be included in the TestAbortedException if the assumption is invalid
message
)

Validate the given assumption.

public static void
assumeTrue(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
,
Supplier<@Nullable String>
the supplier of the message to be included in the TestAbortedException if the assumption is invalid
messageSupplier
)

Validate the given assumption.

public static void
assumingThat(BooleanSupplier
the supplier of the assumption to validate
assumptionSupplier
,
Executable
the block of code to execute if the assumption is valid
executable
)

Execute the supplied Executable, but only if the supplied assumption is valid.

public static void
assumingThat(boolean
the assumption to validate
assumption
,
Executable
the block of code to execute if the assumption is valid
executable
)

Execute the supplied Executable, but only if the supplied assumption is valid.

private static void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Assumptionsback to summary
protected Assumptions()

Protected constructor allowing subclassing but not direct instantiation.

Since
5.3

Method Detail

abortback to summary
public static <V> V abort()

Abort the test without a message.

Although aborting with an explicit message is recommended, this may be useful when maintaining legacy code.

See Javadoc for abort(String) for an explanation of this method's generic return type V.

Annotations
@Contract: -> fail
@API
status:STABLE
since:5.9
@SuppressWarnings:TypeParameterUnusedInFormals
Exceptions
TestAbortedException:
always
Since
5.9
abortback to summary
public static <V> V abort(String message)

Abort the test with the given message.

The generic return type V allows this method to be used directly as a single-statement lambda expression, thereby avoiding the need to implement a code block with an explicit return value. Since this method throws a TestAbortedException before its return statement, this method never actually returns a value to its caller. The following example demonstrates how this may be used in practice.

Stream.of().map(entry -> abort("assumption not met"));
Parameters
message:String

the message to be included in the TestAbortedException

Annotations
@Contract:_ -> fail
@API
status:STABLE
since:5.9
@SuppressWarnings:TypeParameterUnusedInFormals
Exceptions
TestAbortedException:
always
Since
5.9
abortback to summary
public static <V> V abort(Supplier<String> messageSupplier)

Abort the test with the supplied message.

See Javadoc for abort(String) for an explanation of this method's generic return type V.

Parameters
messageSupplier:Supplier<String>

the supplier of the message to be included in the TestAbortedException

Annotations
@Contract:_ -> fail
@API
status:STABLE
since:5.9
@SuppressWarnings:TypeParameterUnusedInFormals
Exceptions
TestAbortedException:
always
Since
5.9
assumeFalseback to summary
public static void assumeFalse(boolean assumption) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

Annotations
@Contract:true -> fail
Exceptions
TestAbortedException:
if the assumption is not false
assumeFalseback to summary
public static void assumeFalse(BooleanSupplier assumptionSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

Exceptions
TestAbortedException:
if the assumption is not false
assumeFalseback to summary
public static void assumeFalse(BooleanSupplier assumptionSupplier, @Nullable String message) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

message:@Nullable String

the message to be included in the TestAbortedException if the assumption is invalid

Exceptions
TestAbortedException:
if the assumption is not false
assumeFalseback to summary
public static void assumeFalse(boolean assumption, Supplier<@Nullable String> messageSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

messageSupplier:Supplier<@Nullable String>

the supplier of the message to be included in the TestAbortedException if the assumption is invalid

Annotations
@Contract:true, _ -> fail
Exceptions
TestAbortedException:
if the assumption is not false
assumeFalseback to summary
public static void assumeFalse(boolean assumption, @Nullable String message) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

message:@Nullable String

the message to be included in the TestAbortedException if the assumption is invalid

Annotations
@Contract:true, _ -> fail
Exceptions
TestAbortedException:
if the assumption is not false
assumeFalseback to summary
public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<@Nullable String> messageSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

messageSupplier:Supplier<@Nullable String>

the supplier of the message to be included in the TestAbortedException if the assumption is invalid

Exceptions
TestAbortedException:
if the assumption is not false
assumeTrueback to summary
public static void assumeTrue(boolean assumption) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

Annotations
@Contract:false -> fail
Exceptions
TestAbortedException:
if the assumption is not true
assumeTrueback to summary
public static void assumeTrue(BooleanSupplier assumptionSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

Exceptions
TestAbortedException:
if the assumption is not true
assumeTrueback to summary
public static void assumeTrue(BooleanSupplier assumptionSupplier, @Nullable String message) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

message:@Nullable String

the message to be included in the TestAbortedException if the assumption is invalid

Exceptions
TestAbortedException:
if the assumption is not true
assumeTrueback to summary
public static void assumeTrue(boolean assumption, Supplier<@Nullable String> messageSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

messageSupplier:Supplier<@Nullable String>

the supplier of the message to be included in the TestAbortedException if the assumption is invalid

Annotations
@Contract:false, _ -> fail
Exceptions
TestAbortedException:
if the assumption is not true
assumeTrueback to summary
public static void assumeTrue(boolean assumption, @Nullable String message) throws TestAbortedException

Validate the given assumption.

Parameters
assumption:boolean

the assumption to validate

message:@Nullable String

the message to be included in the TestAbortedException if the assumption is invalid

Annotations
@Contract:false, _ -> fail
Exceptions
TestAbortedException:
if the assumption is not true
assumeTrueback to summary
public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<@Nullable String> messageSupplier) throws TestAbortedException

Validate the given assumption.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

messageSupplier:Supplier<@Nullable String>

the supplier of the message to be included in the TestAbortedException if the assumption is invalid

Exceptions
TestAbortedException:
if the assumption is not true
assumingThatback to summary
public static void assumingThat(BooleanSupplier assumptionSupplier, Executable executable)

Execute the supplied Executable, but only if the supplied assumption is valid.

Unlike the other assumption methods, this method will not abort the test. If the assumption is invalid, this method does nothing. If the assumption is valid and the executable throws an exception, it will be treated like a regular test failure. That exception will be rethrown as is but masked as an unchecked exception.

Parameters
assumptionSupplier:BooleanSupplier

the supplier of the assumption to validate

executable:Executable

the block of code to execute if the assumption is valid

See Also
assumingThat(boolean, Executable)
assumingThatback to summary
public static void assumingThat(boolean assumption, Executable executable)

Execute the supplied Executable, but only if the supplied assumption is valid.

Unlike the other assumption methods, this method will not abort the test. If the assumption is invalid, this method does nothing. If the assumption is valid and the executable throws an exception, it will be treated like a regular test failure. That exception will be rethrown as is but masked as an unchecked exception.

Parameters
assumption:boolean

the assumption to validate

executable:Executable

the block of code to execute if the assumption is valid

See Also
assumingThat(BooleanSupplier, Executable)
throwAssumptionFailedback to summary
private static void throwAssumptionFailed(@Nullable String message)
Annotations
@Contract:_ -> fail