JUnit 5 provides assumeTrue and assumeFalse in org.junit.jupiter.api.Assumptions to express test preconditions. When a precondition is not met, the test is aborted (skipped) by throwing org.opentest4j.TestAbortedException, rather than failing. Use them to guard environment-dependent tests (OS/JDK, env vars, external services), not to verify business logic.
Method summary
// assumeTrue - continue only if condition is true
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);
// assumeFalse - continue only if condition is false
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);
When to use
- Skip tests based on OS/JDK/architecture conditions.
- Skip when required environment variables or system properties are missing.
- Guard tests that depend on external resources or services.
Examples
void assumeTruePassTest() {
assumeTrue(5 > 2, "Expected 5 > 2");
// If we get here, the assumption passed
assertTrue(true);
}
mvn test -Dtest=AssumeTrueFalseExamples#assumeTruePassTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-true-false>mvn test -Dtest=AssumeTrueFalseExamples#assumeTruePassTest [INFO] Scanning for projects... [INFO] [INFO] -----------< com.logicbig.example:junit-5-assume-true-false >----------- [INFO] Building junit-5-assume-true-false 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-true-false --- [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.AssumeTrueFalseExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.102 s -- in com.logicbig.example.AssumeTrueFalseExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.943 s [INFO] Finished at: 2025-12-02T07:05:01+08:00 [INFO] ------------------------------------------------------------------------
void assumeTrueFailTest() {
// This assumption is intentionally false; the test will be aborted (skipped)
assumeTrue(() -> 2 > 3, () -> "Assumption failed: 2 is not greater than 3");
// The following line won't execute if the assumption fails
assertTrue(false, "Should not reach here when assumption fails");
}
mvn test -Dtest=AssumeTrueFalseExamples#assumeTrueFailTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-true-false>mvn test -Dtest=AssumeTrueFalseExamples#assumeTrueFailTest [INFO] Scanning for projects... [INFO] [INFO] -----------< com.logicbig.example:junit-5-assume-true-false >----------- [INFO] Building junit-5-assume-true-false 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-true-false --- [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.AssumeTrueFalseExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.066 s -- in com.logicbig.example.AssumeTrueFalseExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.142 s [INFO] Finished at: 2025-12-02T07:05:06+08:00 [INFO] ------------------------------------------------------------------------
void assumeFalsePassTest() {
assumeFalse("abc".isEmpty(), "String should not be empty");
assertTrue(true);
}
mvn test -Dtest=AssumeTrueFalseExamples#assumeFalsePassTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-true-false>mvn test -Dtest=AssumeTrueFalseExamples#assumeFalsePassTest [INFO] Scanning for projects... [INFO] [INFO] -----------< com.logicbig.example:junit-5-assume-true-false >----------- [INFO] Building junit-5-assume-true-false 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-true-false --- [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.AssumeTrueFalseExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.090 s -- in com.logicbig.example.AssumeTrueFalseExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.079 s [INFO] Finished at: 2025-12-02T07:05:12+08:00 [INFO] ------------------------------------------------------------------------
void assumeFalseFailTest() {
// Intentionally making assumption fail; test will be skipped
assumeFalse(() -> 10 < 20, () -> "Assumption failed: 10 < 20 is true");
assertTrue(false, "Should not reach here when assumption fails");
}
mvn test -Dtest=AssumeTrueFalseExamples#assumeFalseFailTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-true-false>mvn test -Dtest=AssumeTrueFalseExamples#assumeFalseFailTest [INFO] Scanning for projects... [INFO] [INFO] -----------< com.logicbig.example:junit-5-assume-true-false >----------- [INFO] Building junit-5-assume-true-false 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-true-false --- [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.AssumeTrueFalseExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.063 s -- in com.logicbig.example.AssumeTrueFalseExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.937 s [INFO] Finished at: 2025-12-02T07:05:17+08:00 [INFO] ------------------------------------------------------------------------
void assumeOnEnvironmentExampleTest() {
String os = System.getProperty("os.name", "").toLowerCase();
assumeTrue(os.contains("win"), () -> "Test only relevant on Windows. Current OS: " + os);
assertTrue(os.contains("win"));
}
mvn test -Dtest=AssumeTrueFalseExamples#assumeOnEnvironmentExampleTest OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-assume-true-false>mvn test -Dtest=AssumeTrueFalseExamples#assumeOnEnvironmentExampleTest [INFO] Scanning for projects... [INFO] [INFO] -----------< com.logicbig.example:junit-5-assume-true-false >----------- [INFO] Building junit-5-assume-true-false 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-assume-true-false --- [WARNING] Using platform encoding (Cp1252 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-true-false\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-assume-true-false --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-assume-true-false --- [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.AssumeTrueFalseExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 s -- in com.logicbig.example.AssumeTrueFalseExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.527 s [INFO] Finished at: 2025-12-02T07:05:22+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 17
- Maven 3.9.11
|
|