When tests depend on OS-specific behavior (file paths, process APIs, shells), you can restrict them to certain operating systems.
Overview
@EnabledOnOs runs the test only on the specified OS(es).
@DisabledOnOs skips tests on the specified OS(es).
- OS constants are in
org.junit.jupiter.api.condition.OS.
Quick syntax
@EnabledOnOs(OS.WINDOWS)
void windowsOnly() { /* ... */ }
Java source
Definition of EnabledOnOs(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnabledOnOsCondition.class)
@API(status = STABLE, since = "5.1")
@SuppressWarnings("exports")
public @interface EnabledOnOs {
OS[] value() default {};
@API(status = STABLE, since = "5.9")
String[] architectures() default {};
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Definition of DisabledOnOs(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(DisabledOnOsCondition.class)
@API(status = STABLE, since = "5.1")
@SuppressWarnings("exports")
public @interface DisabledOnOs {
OS[] value() default {};
@API(status = STABLE, since = "5.9")
String[] architectures() default {};
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Example
OS-based conditions
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import static org.junit.jupiter.api.Assertions.assertTrue;
class OsConditionExamplesTest {
@Test
@EnabledOnOs(OS.WINDOWS)
void runs_on_windows_only() {
assertTrue(true);
}
@Test
@DisabledOnOs(OS.LINUX)
void not_run_on_linux() {
assertTrue(true);
}
@Test
void always_runs_control() {
assertTrue(true);
}
}
mvn test -Dtest=OsConditionExamplesTest OutputD:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-enabled-disabled-os>mvn test -Dtest=OsConditionExamplesTest [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:junit-5-enabled-disabled-os >---------- [INFO] Building junit-5-enabled-disabled-os 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-enabled-disabled-os --- [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-enabling-disabling-tests\junit-5-enabled-disabled-os\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-enabled-disabled-os --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-enabled-disabled-os --- [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-enabling-disabling-tests\junit-5-enabled-disabled-os\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-enabled-disabled-os --- [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.5.0:test (default-test) @ junit-5-enabled-disabled-os --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.OsConditionExamplesTest - 0.105 ss [INFO] | +-- [OK] not_run_on_linux - 0.054 ss [INFO] | +-- [OK] runs_on_windows_only - 0.009 ss [INFO] | '-- [OK] always_runs_control - 0 ss [INFO] [INFO] Results: [INFO] [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.628 s [INFO] Finished at: 2025-12-04T00:09:01+08:00 [INFO] ------------------------------------------------------------------------
Running in IntelliJ

Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.1.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|