0
0
JUnittesting~10 mins

@EnabledIfSystemProperty in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a method runs only when a specific system property is set to a certain value. It verifies conditional test execution based on system properties.

Test Code - JUnit 5
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SystemPropertyTest {

    @Test
    @EnabledIfSystemProperty(named = "env", matches = "dev")
    void testRunsOnlyOnDev() {
        String env = System.getProperty("env");
        assertTrue("dev".equals(env));
    }
}
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test framework starts and reads test annotationsJUnit test runner initialized-PASS
2Check system property 'env' valueSystem property 'env' is set to 'dev'System.getProperty('env') == 'dev'PASS
3Since property matches, testRunsOnlyOnDev() is executedInside test methodassertTrue("dev".equals(env))PASS
4Test completes successfullyTest passed-PASS
Failure Scenario
Failing Condition: System property 'env' is not set or does not equal 'dev'
Execution Trace Quiz - 3 Questions
Test your understanding
What causes the test method to run?
ANo system property is set
BSystem property 'env' equals 'dev'
CSystem property 'env' equals 'prod'
DTest method has no annotations
Key Result
Use @EnabledIfSystemProperty to run tests only when specific system properties are set, helping to control test execution in different environments.