import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class OSTest { @Test @EnabledOnOs(OS.WINDOWS) void testOnlyOnWindows() { System.out.println("Running Windows-specific test"); } }
The annotation @EnabledOnOs(OS.WINDOWS) causes the test to run only on Windows OS. On Linux, the test is skipped and not executed.
String osName = System.getProperty("os.name").toLowerCase();System property "os.name" returns a string like "Linux" or "linux". Using toLowerCase() and contains("linux") is a safe way to check.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class MacTest { @Test @EnabledOnOs(os = OS.MAC) void testMacOnly() { System.out.println("Running Mac test"); } }
The correct attribute name for specifying OS in @EnabledOnOs is 'value', not 'os'. Using 'os' causes the annotation to ignore the condition and run the test on all OS.
The @EnabledOnOs annotation accepts an array of OS enums. The correct syntax is @EnabledOnOs({OS.WINDOWS, OS.LINUX}).
When @EnabledOnOs is applied at the class level, it controls execution of all test methods inside. If the OS does not match, all tests are skipped.
