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.
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.
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)); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test framework starts and reads test annotations | JUnit test runner initialized | — | PASS |
| 2 | Check system property 'env' value | System property 'env' is set to 'dev' | System.getProperty('env') == 'dev' | PASS |
| 3 | Since property matches, testRunsOnlyOnDev() is executed | Inside test method | assertTrue("dev".equals(env)) | PASS |
| 4 | Test completes successfully | Test passed | — | PASS |
@EnabledIfSystemProperty annotation in JUnit tests?env equals prod using @EnabledIfSystemProperty?named for the system property name and matches for the regex pattern to match.named and matches correctly. Other options use incorrect parameter names.@EnabledIfSystemProperty(named = "os.name", matches = "Windows.*")
void testWindowsFeature() {
System.out.println("Windows test executed");
}os.name is set to Windows 10, what will be the output when running this test?os.name matches the regex Windows.*. The value Windows 10 matches this pattern.@EnabledIfSystemProperty(named = "user.language", matches = "en")
void testEnglishFeature() {
// test code
}user.language is set to en. What is the most likely cause?user.language is not set before running tests, the condition fails and test is skipped.env is set to either dev or test. Which @EnabledIfSystemProperty annotation correctly implements this?dev|test means match either "dev" or "test" exactly.Pass the test and this pattern moves unknown