Verify feature behavior only on Windows OS
Preconditions (2)
✅ Expected Result: The test runs and passes only on Windows OS. On other OS, the test is skipped and does not fail.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.assertEquals; public class OperatingSystemTest { @Test @EnabledOnOs(OS.WINDOWS) void testRunsOnlyOnWindows() { String osName = "Windows"; assertEquals("Windows", osName, "Test should run only on Windows OS"); } }
The test class OperatingSystemTest contains one test method testRunsOnlyOnWindows.
This method is annotated with @EnabledOnOs(OS.WINDOWS), which means JUnit will run this test only if the OS is Windows.
Inside the test, we assert that the string osName equals "Windows". This simple assertion confirms the test logic.
If you run this test on Windows, it will execute and pass. On other OS like Linux or Mac, JUnit will skip this test without failing it.
This approach keeps OS-specific tests clean and avoids manual OS checks inside the test code.
Now add tests that run only on Linux and Mac OS using @EnabledOnOs with multiple OS values
@EnabledOnOs annotation in JUnit?@EnabledOnOs@EnabledOnOs?@EnabledOnOs annotation uses the enum OS with constants like WINDOWS.@EnabledOnOs(OS.WINDOWS) uses the correct enum constant OS.WINDOWS. Options A, B, and C use invalid syntax or strings instead of enum constants.@Test
@EnabledOnOs(OS.WINDOWS)
void testOnlyOnWindows() {
System.out.println("Running Windows test");
}@EnabledOnOs if the current OS does not match.@Test
@EnabledOnOs(OS.LINUX, OS.MAC)
void testOnUnix() {
// test code
}@EnabledOnOs annotation expects an array of OS enums when specifying multiple OSes.@EnabledOnOs({OS.LINUX, OS.MAC}). The code misses curly braces for the array. Thus, Multiple OS values must be inside an array correctly identifies that multiple OS values must be inside an array.@EnabledOnOs annotation for this requirement?@EnabledOnOs.OS.WINDOWS and OS.MAC. @EnabledOnOs(OS.WINDOWS, OS.MAC, OS.LINUX) includes Linux, which should be excluded. @EnabledOnOs(OS.WINDOWS) @EnabledOnOs(OS.MAC) uses multiple annotations, which is invalid. @EnabledOnOs(OS.WINDOWS & OS.MAC) uses an invalid bitwise operator.Pass the test and this pattern moves unknown