0
0
JUnittesting~15 mins

@EnabledIfSystemProperty in JUnit - Build an Automation Script

Choose your learning style9 modes available
Test runs only if system property 'env' equals 'test'
Preconditions (2)
Step 1: Set system property 'env' to 'test'
Step 2: Run the test class containing the conditional test method
Step 3: Observe that the test method annotated with @EnabledIfSystemProperty runs
Step 4: Set system property 'env' to 'prod'
Step 5: Run the test class again
Step 6: Observe that the test method annotated with @EnabledIfSystemProperty is skipped
✅ Expected Result: The test method runs only when system property 'env' is 'test'. It is skipped otherwise.
Automation Requirements - JUnit 5
Assertions Needed:
Verify test method executes only when system property 'env' equals 'test'
Verify test method is skipped when system property 'env' is not 'test'
Best Practices:
Use @EnabledIfSystemProperty annotation correctly with 'named' and 'matches' attributes
Use assertions inside the test to confirm execution
Use @BeforeAll or @BeforeEach to set or clear system properties if needed
Keep tests independent and clean up system properties after tests
Automated Solution
JUnit
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class EnabledIfSystemPropertyTest {

    @BeforeEach
    void clearProperty() {
        System.clearProperty("env");
    }

    @Test
    @EnabledIfSystemProperty(named = "env", matches = "test")
    void testRunsOnlyIfEnvIsTest() {
        // This test runs only if system property 'env' equals 'test'
        String env = System.getProperty("env");
        assertTrue("test".equals(env), "System property 'env' should be 'test'");
    }

    @Test
    void testAlwaysRuns() {
        // Control test to show normal test execution
        assertTrue(true);
    }

    public static void main(String[] args) {
        // Example to run tests with system property set
        System.setProperty("env", "test");
        org.junit.platform.launcher.LauncherDiscoveryRequest request =
                org.junit.platform.engine.discovery.DiscoverySelectors.selectClass(EnabledIfSystemPropertyTest.class);
        // Normally tests run via IDE or build tool
    }
}

The test class EnabledIfSystemPropertyTest contains a test method testRunsOnlyIfEnvIsTest annotated with @EnabledIfSystemProperty. This annotation makes the test run only if the system property named env matches the value test.

Before each test, the system property env is cleared to avoid side effects. To run the conditional test, set the system property env to test before running the tests.

The assertion inside the test confirms that the system property is indeed test when the test runs.

Another test testAlwaysRuns is included to show a normal test that always runs.

This setup helps beginners understand how to use @EnabledIfSystemProperty to conditionally run tests based on environment settings.

Common Mistakes - 4 Pitfalls
Not setting the system property before running the test
Using incorrect property name or value in @EnabledIfSystemProperty
Not clearing or resetting system properties after tests
Putting assertions outside the conditional test method
Bonus Challenge

Now add data-driven testing with 3 different system property values ('test', 'dev', 'prod') and verify which tests run.

Show Hint