JUnit Jupiter provides APIs in org.junit.jupiter.api.Assumptions to abort the current test intentionally and mark it as skipped (aborted) rather than failed. The primary API is abort(...).
Method summary
// org.junit.jupiter.api.Assumptions
public static void abort()
public static void abort(java.lang.String message)
public static void abort(java.util.function.Supplier<java.lang.String> messageSupplier)
Behavior
abort(...) aborts the current test by throwing TestAbortedException.
- Aborted tests are reported as skipped (or aborted) - not failures.
- Use these APIs for environment-dependent scenarios (OS/JDK/version), missing external resources/services, or feature-flagged functionality that is not active.
- Prefer providing a meaningful message to help readers understand why the test was aborted.
Examples
void abortWithoutMessage() {
abort();
// Any code below is not executed.
}
mvn test -Dtest=AssumptionsAbortExamples#abortWithoutMessage OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-manually-skipping-tests>mvn test -Dtest=AssumptionsAbortExamples#abortWithoutMessage [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-manually-skipping-tests >-------- [INFO] Building junit-5-manually-skipping-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-manually-skipping-tests --- [INFO] Recompiling the module because of added or removed source files. [INFO] Compiling 1 source file with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-manually-skipping-tests --- [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.AssumptionsAbortExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.076 s -- in com.logicbig.example.AssumptionsAbortExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.119 s [INFO] Finished at: 2025-12-02T07:55:16+08:00 [INFO] ------------------------------------------------------------------------
void abortWithStringMessage() {
abort("Skipping this test manually using Assumptions.abort(String)");
}
mvn test -Dtest=AssumptionsAbortExamples#abortWithStringMessage OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-manually-skipping-tests>mvn test -Dtest=AssumptionsAbortExamples#abortWithStringMessage [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-manually-skipping-tests >-------- [INFO] Building junit-5-manually-skipping-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-manually-skipping-tests --- [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.AssumptionsAbortExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.050 s -- in com.logicbig.example.AssumptionsAbortExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.092 s [INFO] Finished at: 2025-12-02T07:55:20+08:00 [INFO] ------------------------------------------------------------------------
void abortWithSupplierMessage() {
abort(() -> "Skipped at " + System.currentTimeMillis());
}
mvn test -Dtest=AssumptionsAbortExamples#abortWithSupplierMessage OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-manually-skipping-tests>mvn test -Dtest=AssumptionsAbortExamples#abortWithSupplierMessage [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-manually-skipping-tests >-------- [INFO] Building junit-5-manually-skipping-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-manually-skipping-tests --- [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.AssumptionsAbortExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.050 s -- in com.logicbig.example.AssumptionsAbortExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.799 s [INFO] Finished at: 2025-12-02T07:55:23+08:00 [INFO] ------------------------------------------------------------------------
void abortConditionallyBasedOnEnv() {
String token = System.getenv("EXAMPLE_TOKEN");
if (token == null || token.isBlank()) {
abort("Required environment variable EXAMPLE_TOKEN not set; skipping test");
}
// If the env var exists, additional test logic would go here.
}
mvn test -Dtest=AssumptionsAbortExamples#abortConditionallyBasedOnEnv OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-manually-skipping-tests>mvn test -Dtest=AssumptionsAbortExamples#abortConditionallyBasedOnEnv [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-manually-skipping-tests >-------- [INFO] Building junit-5-manually-skipping-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-manually-skipping-tests --- [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.AssumptionsAbortExamples [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.039 s -- in com.logicbig.example.AssumptionsAbortExamples [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.607 s [INFO] Finished at: 2025-12-02T07:55:26+08:00 [INFO] ------------------------------------------------------------------------
void testForWindowsOS() {
String osName = System.getProperty("os.name").toLowerCase();
if (!osName.contains("windows")) {
// Abort if not running on Windows.
abort("Aborting test: requires Windows Operating System");
}
// This code block runs exclusively on Windows machines.
// In a real scenario, this would contain Windows-specific logic (e.g., file path checks).
assertTrue(osName.contains("windows"), "This assertion should only run on Windows");
assertFalse(osName.contains("mac"), "Should not be on Mac if we are here");
}
mvn test -Dtest=AssumptionsAbortExamples#testForWindowsOS OutputD:\example-projects\junit-5\junit-5-assumptions\junit-5-manually-skipping-tests>mvn test -Dtest=AssumptionsAbortExamples#testForWindowsOS [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-manually-skipping-tests >-------- [INFO] Building junit-5-manually-skipping-tests 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-manually-skipping-tests --- [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-manually-skipping-tests\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-manually-skipping-tests --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-manually-skipping-tests --- [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.AssumptionsAbortExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 s -- in com.logicbig.example.AssumptionsAbortExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.655 s [INFO] Finished at: 2025-12-02T07:55:28+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.9.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|