In JUnit 5, an assumption is a precondition for a test. If an assumption is not met, the test is aborted (skipped) rather than marked as failed. This is useful when a test should only run under certain environments or external conditions (e.g., on a particular OS, when an environment variable is set, or when a network/service is available).
Assumptions live in org.junit.jupiter.api.Assumptions. Let's see what methods this class has.
assumeTrue and assumeFalse
public static void assumeTrue(boolean assumption);
public static void assumeTrue(BooleanSupplier assumptionSupplier);
public static void assumeTrue(BooleanSupplier assumptionSupplier, @Nullable String message);
public static void assumeTrue(boolean assumption, Supplier<@Nullable String> messageSupplier);
public static void assumeTrue(boolean assumption, @Nullable String message);
public static void assumeTrue(BooleanSupplier assumptionSupplier,
Supplier<@Nullable String> messageSupplier);
public static void assumeFalse(boolean assumption);
public static void assumeFalse(BooleanSupplier assumptionSupplier);
public static void assumeFalse(BooleanSupplier assumptionSupplier, @Nullable String message);
public static void assumeFalse(boolean assumption, Supplier<@Nullable String> messageSupplier);
public static void assumeFalse(boolean assumption, @Nullable String message);
public static void assumeFalse(BooleanSupplier assumptionSupplier,
Supplier<@Nullable String> messageSupplier);
Above methods are used to skip test if certain condition is true or false. Each of the above method throws TestAbortedException which is an unchecked exception. When we use call an assumption method and if it fails, the method call internally throws an org.opentest4j.TestAbortedException. This exception is caught by the JUnit test engine and handled in a specific way: the test execution is aborted, not marked as a failure.
assumingThat methods
public static void assumingThat(BooleanSupplier assumptionSupplier, Executable executable);
public static void assumingThat(boolean assumption, Executable executable);
Above methods can be used if we want to skip only certain part of code (code within Executable) and continue the rest.
abort() methods
public static <V> V abort();
public static <V> V abort(String message);
public static <V> V abort(Supplier<String> messageSupplier);
Above methods are used to manually skip the test.
How assumptions differ from assertions
- Assertions (from
org.junit.jupiter.api.Assertions) fail a test when a condition is not satisfied.
- Assumptions abort (skip) a test when a condition is not satisfied.
- Use assumptions to guard environment-dependent tests, not to verify production logic.
Typical use cases
- Enable tests only on specific OS/JDK versions or architectures.
- Skip tests when required environment variables or system properties are missing.
- Skip tests when external resources (DB, message broker, network) are not available.
- Run a subset of assertions conditionally with
assumingThat.
In next tutorials we will see some examples.
|