This annotation helps run tests only when a specific system property has a certain value. It avoids running tests in the wrong environment.
@EnabledIfSystemProperty in JUnit
@EnabledIfSystemProperty(named = "propertyName", matches = "valueRegex")
The named attribute specifies the system property key.
The matches attribute uses a regular expression to match the property value.
env equals exactly dev.@EnabledIfSystemProperty(named = "env", matches = "dev") void testOnlyOnDev() { // test code }
os.name property.@EnabledIfSystemProperty(named = "os.name", matches = ".*Windows.*") void testOnlyOnWindows() { // test code }
The first test runs only if the system property runSpecialTests is set to true. The second test always runs.
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 = "runSpecialTests", matches = "true") void testRunsOnlyIfPropertyTrue() { assertTrue("true".equals(System.getProperty("runSpecialTests"))); } @Test void testAlwaysRuns() { assertTrue(true); } }
System properties can be set when running tests using JVM arguments like -DrunSpecialTests=true.
If the property is missing or does not match, the test is skipped, not failed.
This helps keep tests clean and environment-aware without manual checks inside test code.
@EnabledIfSystemProperty runs tests only when a system property matches a pattern.
It helps control test execution based on environment settings.
Use it to skip or run tests automatically depending on where and how tests run.
Practice
@EnabledIfSystemProperty annotation in JUnit tests?Solution
Step 1: Understand the annotation's purpose
<code>@EnabledIfSystemPropertyStep 2: Analyze the options
To run a test only if a specific system property matches a given value correctly states that the test runs only if a system property matches a value. Options B, C, and D describe unrelated behaviors.Final Answer:
To run a test only if a specific system property matches a given value -> Option CQuick Check:
@EnabledIfSystemProperty = Conditional test run [OK]
- Confusing it with @Disabled or @Enabled annotations
- Thinking it runs tests based on date or time
- Assuming it runs tests multiple times automatically
env equals prod using @EnabledIfSystemProperty?Solution
Step 1: Recall the annotation parameters
The correct parameters arenamedfor the system property name andmatchesfor the regex pattern to match.Step 2: Compare options with correct syntax
@EnabledIfSystemProperty(named = "env", matches = "prod") usesnamedandmatchescorrectly. Other options use incorrect parameter names.Final Answer:
@EnabledIfSystemProperty(named = "env", matches = "prod") -> Option AQuick Check:
named + matches = correct syntax [OK]
- Using 'name' instead of 'named'
- Using 'value' or 'equals' instead of 'matches'
- Mixing parameter names incorrectly
@EnabledIfSystemProperty(named = "os.name", matches = "Windows.*")
void testWindowsFeature() {
System.out.println("Windows test executed");
}If the system property
os.name is set to Windows 10, what will be the output when running this test?Solution
Step 1: Understand the system property condition
The test runs only ifos.namematches the regexWindows.*. The valueWindows 10matches this pattern.Step 2: Predict test behavior
Since the condition is true, the test runs and prints "Windows test executed".Final Answer:
Windows test executed -> Option BQuick Check:
Property matches regex = test runs [OK]
- Assuming test skips because of partial match
- Expecting exceptions from annotation
- Confusing output with test failure
@EnabledIfSystemProperty(named = "user.language", matches = "en")
void testEnglishFeature() {
// test code
}The test never runs even when the system property
user.language is set to en. What is the most likely cause?Solution
Step 1: Check system property availability
If the system propertyuser.languageis not set before running tests, the condition fails and test is skipped.Step 2: Validate other options
The regex pattern should be "en.*" to match properly is unnecessary because exact match "en" works. The annotation parameter should be 'name' not 'named' is incorrect because 'named' is the correct parameter. JUnit does not support @EnabledIfSystemProperty is false; JUnit supports this annotation.Final Answer:
The system property is not set before test execution -> Option AQuick Check:
Property must be set before test runs [OK]
- Confusing 'named' with 'name' parameter
- Assuming regex must be partial match
- Believing JUnit lacks support for this annotation
env is set to either dev or test. Which @EnabledIfSystemProperty annotation correctly implements this?Solution
Step 1: Understand regex for multiple options
The regexdev|testmeans match either "dev" or "test" exactly.Step 2: Evaluate each option's regex
@EnabledIfSystemProperty(named = "env", matches = "dev|test") uses correct regex with alternation. @EnabledIfSystemProperty(named = "env", matches = "dev,test") uses comma which matches literal comma, not alternation. @EnabledIfSystemProperty(named = "env", matches = "[dev,test]") uses square brackets which match single characters, not words. @EnabledIfSystemProperty(named = "env", matches = "dev?test") uses question mark which means optional character, not alternation.Final Answer:
@EnabledIfSystemProperty(named = "env", matches = "dev|test") -> Option DQuick Check:
Use regex alternation with | for multiple matches [OK]
- Using commas instead of | in regex
- Using square brackets which match single chars
- Misusing ? for alternation
Ready to prove it?
Pass the test and this pattern moves unknown
