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.
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:
-
Constructor Summary
ModifierConstructorDescriptionprotected
Protected constructor allowing subclassing but not direct instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> V
abort()
Abort the test without a message.static <V> V
Abort the test with the givenmessage
.static <V> V
Abort the test with the supplied message.static void
assumeFalse
(boolean assumption) Validate the given assumption.static void
assumeFalse
(boolean assumption, String message) Validate the given assumption.static void
assumeFalse
(boolean assumption, Supplier<String> messageSupplier) Validate the given assumption.static void
assumeFalse
(BooleanSupplier assumptionSupplier) Validate the given assumption.static void
assumeFalse
(BooleanSupplier assumptionSupplier, String message) Validate the given assumption.static void
assumeFalse
(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) Validate the given assumption.static void
assumeTrue
(boolean assumption) Validate the given assumption.static void
assumeTrue
(boolean assumption, String message) Validate the given assumption.static void
assumeTrue
(boolean assumption, Supplier<String> messageSupplier) Validate the given assumption.static void
assumeTrue
(BooleanSupplier assumptionSupplier) Validate the given assumption.static void
assumeTrue
(BooleanSupplier assumptionSupplier, String message) Validate the given assumption.static void
assumeTrue
(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) Validate the given assumption.static void
assumingThat
(boolean assumption, Executable executable) Execute the suppliedExecutable
, but only if the supplied assumption is valid.static void
assumingThat
(BooleanSupplier assumptionSupplier, Executable executable) Execute the suppliedExecutable
, but only if the supplied assumption is valid.
-
Constructor Details
-
Assumptions
protected Assumptions()Protected constructor allowing subclassing but not direct instantiation.- Since:
- 5.3
-
-
Method Details
-
assumeTrue
Validate the given assumption.- Parameters:
assumption
- the assumption to validate- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeTrue
Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validate- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeTrue
public static void assumeTrue(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validatemessage
- the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeTrue
public static void assumeTrue(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException Validate the given assumption.- Parameters:
assumption
- the assumption to validatemessageSupplier
- the supplier of the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeTrue
Validate the given assumption.- Parameters:
assumption
- the assumption to validatemessage
- the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeTrue
public static void assumeTrue(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validatemessageSupplier
- the supplier of the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is nottrue
-
assumeFalse
Validate the given assumption.- Parameters:
assumption
- the assumption to validate- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumeFalse
Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validate- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumeFalse
public static void assumeFalse(BooleanSupplier assumptionSupplier, String message) throws TestAbortedException Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validatemessage
- the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumeFalse
public static void assumeFalse(boolean assumption, Supplier<String> messageSupplier) throws TestAbortedException Validate the given assumption.- Parameters:
assumption
- the assumption to validatemessageSupplier
- the supplier of the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumeFalse
Validate the given assumption.- Parameters:
assumption
- the assumption to validatemessage
- the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumeFalse
public static void assumeFalse(BooleanSupplier assumptionSupplier, Supplier<String> messageSupplier) throws TestAbortedException Validate the given assumption.- Parameters:
assumptionSupplier
- the supplier of the assumption to validatemessageSupplier
- the supplier of the message to be included in theTestAbortedException
if the assumption is invalid- Throws:
TestAbortedException
- if the assumption is notfalse
-
assumingThat
Execute the suppliedExecutable
, 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 butmasked
as an unchecked exception.- Parameters:
assumptionSupplier
- the supplier of the assumption to validateexecutable
- the block of code to execute if the assumption is valid- See Also:
-
assumingThat
Execute the suppliedExecutable
, 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 butmasked
as an unchecked exception.- Parameters:
assumption
- the assumption to validateexecutable
- the block of code to execute if the assumption is valid- See Also:
-
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 typeV
.- Throws:
TestAbortedException
- always- Since:
- 5.9
-
abort
Abort the test with the givenmessage
.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 aTestAbortedException
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
- the message to be included in theTestAbortedException
- Throws:
TestAbortedException
- always- Since:
- 5.9
-
abort
Abort the test with the supplied message.See Javadoc for
abort(String)
for an explanation of this method's generic return typeV
.- Parameters:
messageSupplier
- the supplier of the message to be included in theTestAbortedException
- Throws:
TestAbortedException
- always- Since:
- 5.9
-