assumingThat from org.junit.jupiter.api.Assumptions lets you run a part of a test only when a condition holds. Unlike assumeTrue/assumeFalse, it does not abort the whole test when the condition is false; it simply skips the provided executable block and continues with the rest of the test.
Method summary
// Run the executable only if condition evaluates to true
public static void assumingThat(boolean condition, org.junit.jupiter.api.function.Executable executable)
public static void assumingThat(java.util.function.BooleanSupplier condition, org.junit.jupiter.api.function.Executable executable)
Behavior
- If the condition is true, the given block (executable) is executed.
- If the condition is false, the block is skipped and the test continues.
- No
org.opentest4j.TestAbortedException is thrown by assumingThat itself when the condition is false. The test only aborts if the executable contains other assumptions that fail.
When to use
- Wrap OS/JDK/architecture-specific checks without skipping the whole test.
- Run additional verifications on CI while keeping local runs fast.
- Guard optional, environment-dependent assertions (feature flags, env vars, external services).
- Keep core assertions always-on and surround optional parts with
assumingThat.
Examples
void assumingThatPassTest() {
AtomicBoolean ran = new AtomicBoolean(false);
assumingThat(5 > 2, () -> ran.set(true));
assertTrue(ran.get(), "executable should run when condition is true");
}
mvn test -Dtest=AssumeThatExamples#assumingThatPassTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that>mvn test -Dtest=AssumeThatExamples#assumingThatPassTest [INFO] Scanning for projects... [INFO] [INFO] --------------< com.logicbig.example:junit-5-assume-that >-------------- [INFO] Building junit-5-assume-that 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-that --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.AssumeThatExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.070 s -- in com.logicbig.example.AssumeThatExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.314 s [INFO] Finished at: 2025-12-02T00:33:43+08:00 [INFO] ------------------------------------------------------------------------
void assumingThatFailTest() {
AtomicBoolean ran = new AtomicBoolean(false);
// condition is false, executable should not run, but the test must proceed
assumingThat(2 > 3, () -> ran.set(true));
assertFalse(ran.get(), "executable should not run when condition is false");
// the test still continues and can make further assertions
assertTrue(true);
}
mvn test -Dtest=AssumeThatExamples#assumingThatFailTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that>mvn test -Dtest=AssumeThatExamples#assumingThatFailTest [INFO] Scanning for projects... [INFO] [INFO] --------------< com.logicbig.example:junit-5-assume-that >-------------- [INFO] Building junit-5-assume-that 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-that --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.AssumeThatExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.110 s -- in com.logicbig.example.AssumeThatExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.886 s [INFO] Finished at: 2025-12-02T00:35:00+08:00 [INFO] ------------------------------------------------------------------------
void assumingThatWithBooleanSupplier() {
AtomicBoolean ran = new AtomicBoolean(false);
assumingThat(() -> "junit".startsWith("ju"), () -> ran.set(true));
assertTrue(ran.get());
}
mvn test -Dtest=AssumeThatExamples#assumingThatWithBooleanSupplier OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that>mvn test -Dtest=AssumeThatExamples#assumingThatWithBooleanSupplier [INFO] Scanning for projects... [INFO] [INFO] --------------< com.logicbig.example:junit-5-assume-that >-------------- [INFO] Building junit-5-assume-that 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-that --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.AssumeThatExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 s -- in com.logicbig.example.AssumeThatExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.941 s [INFO] Finished at: 2025-12-02T00:33:54+08:00 [INFO] ------------------------------------------------------------------------
void assumingThatWithEnvironmentSpecificCheck() {
String os = System.getProperty("os.name", "").toLowerCase(Locale.ROOT);
AtomicBoolean executed = new AtomicBoolean(false);
// Only execute the block when running on Windows
assumingThat(os.contains("win"), () -> executed.set(true));
if (os.contains("win")) {
assertTrue(executed.get(), "block should execute on Windows");
} else {
assertFalse(executed.get(), "block should not execute on non-Windows");
}
}
mvn test -Dtest=AssumeThatExamples#assumingThatWithEnvironmentSpecificCheck OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that>mvn test -Dtest=AssumeThatExamples#assumingThatWithEnvironmentSpecificCheck [INFO] Scanning for projects... [INFO] [INFO] --------------< com.logicbig.example:junit-5-assume-that >-------------- [INFO] Building junit-5-assume-that 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-that --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.AssumeThatExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 s -- in com.logicbig.example.AssumeThatExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.485 s [INFO] Finished at: 2025-12-02T00:33:59+08:00 [INFO] ------------------------------------------------------------------------
void assumingThatNotAbortTestOnFalse() {
// Demonstrate that assumingThat does not abort; it only skips the block
AtomicBoolean sideEffect = new AtomicBoolean(false);
assumingThat(false, () -> sideEffect.set(true));
// still reaches here
assertFalse(sideEffect.get());
}
mvn test -Dtest=AssumeThatExamples#assumingThatNotAbortTestOnFalse OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that>mvn test -Dtest=AssumeThatExamples#assumingThatNotAbortTestOnFalse [INFO] Scanning for projects... [INFO] [INFO] --------------< com.logicbig.example:junit-5-assume-that >-------------- [INFO] Building junit-5-assume-that 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-that --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-that\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-that --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-that --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.AssumeThatExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.100 s -- in com.logicbig.example.AssumeThatExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.474 s [INFO] Finished at: 2025-12-02T00:34:05+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|
|