0
0
JUnittesting~10 mins

@EnabledOnOs for OS-specific tests in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit 5
JUnit
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");
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and detects the test method annotated with @EnabledOnOs(OS.WINDOWS)JUnit test environment initialized-PASS
2JUnit checks the current OS from System.getProperty("os.name")System property os.name is read, e.g., "Windows 10" or "Linux"-PASS
3JUnit compares current OS with the annotation condition (Windows)OS matches Windows if running on Windows, else does not match-PASS
4If OS matches Windows, test method executes and asserts os.name contains "windows"Test method runs on Windows OSassertTrue(osName.contains("windows"))PASS
5If OS does not match Windows, test is skipped by JUnitTest method is not executed on non-Windows OS-PASS
Failure Scenario
Failing Condition: Test runs on a non-Windows OS because annotation is missing or misconfigured
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @EnabledOnOs(OS.WINDOWS) annotation do in this test?
ARuns the test on all operating systems
BRuns the test only on Windows OS
CDisables the test on Windows OS
DRuns the test only on Linux OS
Key Result
Use @EnabledOnOs to run tests only on specific operating systems, preventing irrelevant test failures on other platforms.