Test Overview
This test checks that a method runs only on Windows OS using the @EnabledOnOs annotation. It verifies the test is skipped on other operating systems.
This test checks that a method runs only on Windows OS using the @EnabledOnOs annotation. It verifies the test is skipped on other operating systems.
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.assertTrue; public class OsSpecificTest { @Test @EnabledOnOs(OS.WINDOWS) void testRunsOnlyOnWindows() { String osName = System.getProperty("os.name").toLowerCase(); assertTrue(osName.contains("windows"), "Test should run only on Windows OS"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and detects the test method annotated with @EnabledOnOs(OS.WINDOWS) | JUnit test environment initialized | — | PASS |
| 2 | JUnit checks the current OS from System.getProperty("os.name") | System property os.name is read, e.g., "Windows 10" or "Linux" | — | PASS |
| 3 | JUnit compares current OS with the annotation condition (Windows) | OS matches Windows if running on Windows, else does not match | — | PASS |
| 4 | If OS matches Windows, test method executes and asserts os.name contains "windows" | Test method runs on Windows OS | assertTrue(osName.contains("windows")) | PASS |
| 5 | If OS does not match Windows, test is skipped by JUnit | Test method is not executed on non-Windows OS | — | PASS |
@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