Bird
Raised Fist0
JUnittesting~20 mins

@EnabledIfSystemProperty in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
System Property Conditional Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding @EnabledIfSystemProperty usage
What does the JUnit annotation @EnabledIfSystemProperty(named = "os.name", matches = "Windows.*") do when applied to a test method?
ARuns the test only if the operating system name starts with "Windows".
BRuns the test only if the system property "os.name" is exactly "Windows".
CDisables the test if the operating system name contains "Windows" anywhere.
DRuns the test regardless of the operating system.
Attempts:
2 left
💡 Hint
Think about how the matches attribute uses regular expressions.
❓ Predict Output
intermediate
2:00remaining
Test execution with @EnabledIfSystemProperty
Given the following test class, what will be the output when running on a Linux system where os.name is "Linux"?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

public class SystemPropertyTest {

    @Test
    @EnabledIfSystemProperty(named = "os.name", matches = "Windows.*")
    void testWindowsOnly() {
        System.out.println("Windows test executed");
    }

    @Test
    void testAlways() {
        System.out.println("Always executed");
    }
}
ABoth "Windows test executed" and "Always executed" are printed.
BOnly "Always executed" is printed.
COnly "Windows test executed" is printed.
DNo output is printed because tests are skipped.
Attempts:
2 left
💡 Hint
Check which tests are enabled based on the system property.
❓ assertion
advanced
1:30remaining
Assertion on test enablement with @EnabledIfSystemProperty
Which assertion correctly verifies that a test method annotated with @EnabledIfSystemProperty(named = "env", matches = "prod") runs only when the system property env is set to "prod"?
AassertTrue(System.getProperty("env").equals("prod"));
BassertTrue(System.getProperty("env").contains("prod"));
CassertFalse(System.getProperty("env").equals("prod"));
DassertNull(System.getProperty("env"));
Attempts:
2 left
💡 Hint
The annotation requires exact match to "prod".
🔧 Debug
advanced
2:00remaining
Debugging skipped test with @EnabledIfSystemProperty
A test annotated with @EnabledIfSystemProperty(named = "user.language", matches = "en") is unexpectedly skipped on a machine where user.language is "en_US". What is the most likely cause?
AThe annotation requires the property to be null to run.
BThe system property "user.language" is not set correctly.
CThe regex "en" does not match "en_US" because it expects an exact match.
DJUnit does not support regex in @EnabledIfSystemProperty.
Attempts:
2 left
💡 Hint
Consider how regex matching works in Java.
❓ framework
expert
2:30remaining
Combining @EnabledIfSystemProperty with other conditional annotations
Consider a test method annotated with both @EnabledIfSystemProperty(named = "env", matches = "prod") and @EnabledOnOs(OS.WINDOWS). Under which condition will this test run?
AWhen either the system property "env" is "prod" or the OS is Windows.
BNever runs because the annotations conflict.
CAlways runs regardless of system property or OS.
DOnly when the system property "env" is "prod" and the OS is Windows.
Attempts:
2 left
💡 Hint
Think about how multiple conditional annotations combine in JUnit.

Practice

(1/5)
1. What is the primary purpose of the @EnabledIfSystemProperty annotation in JUnit tests?
easy
A. To enable a test only on weekends
B. To disable a test permanently
C. To run a test only if a specific system property matches a given value
D. To run a test multiple times automatically

Solution

  1. Step 1: Understand the annotation's purpose

    <code>@EnabledIfSystemProperty
  2. Step 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.
  3. Final Answer:

    To run a test only if a specific system property matches a given value -> Option C
  4. Quick Check:

    @EnabledIfSystemProperty = Conditional test run [OK]
Hint: It runs tests only when system property matches [OK]
Common Mistakes:
  • Confusing it with @Disabled or @Enabled annotations
  • Thinking it runs tests based on date or time
  • Assuming it runs tests multiple times automatically
2. Which of the following is the correct syntax to enable a test only when the system property env equals prod using @EnabledIfSystemProperty?
easy
A. @EnabledIfSystemProperty(named = "env", matches = "prod")
B. @EnabledIfSystemProperty(name = "env", value = "prod")
C. @EnabledIfSystemProperty(property = "env", equals = "prod")
D. @EnabledIfSystemProperty(key = "env", match = "prod")

Solution

  1. Step 1: Recall the annotation parameters

    The correct parameters are named for the system property name and matches for the regex pattern to match.
  2. Step 2: Compare options with correct syntax

    @EnabledIfSystemProperty(named = "env", matches = "prod") uses named and matches correctly. Other options use incorrect parameter names.
  3. Final Answer:

    @EnabledIfSystemProperty(named = "env", matches = "prod") -> Option A
  4. Quick Check:

    named + matches = correct syntax [OK]
Hint: Use named and matches parameters exactly [OK]
Common Mistakes:
  • Using 'name' instead of 'named'
  • Using 'value' or 'equals' instead of 'matches'
  • Mixing parameter names incorrectly
3. Consider the following JUnit test snippet:
@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?
medium
A. Test skipped silently
B. Windows test executed
C. Runtime exception occurs
D. Test fails with assertion error

Solution

  1. Step 1: Understand the system property condition

    The test runs only if os.name matches the regex Windows.*. The value Windows 10 matches this pattern.
  2. Step 2: Predict test behavior

    Since the condition is true, the test runs and prints "Windows test executed".
  3. Final Answer:

    Windows test executed -> Option B
  4. Quick Check:

    Property matches regex = test runs [OK]
Hint: Regex match true means test runs and prints output [OK]
Common Mistakes:
  • Assuming test skips because of partial match
  • Expecting exceptions from annotation
  • Confusing output with test failure
4. Given this test method:
@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?
medium
A. The system property is not set before test execution
B. The regex pattern should be "en.*" to match properly
C. The annotation parameter should be 'name' not 'named'
D. JUnit does not support @EnabledIfSystemProperty

Solution

  1. Step 1: Check system property availability

    If the system property user.language is not set before running tests, the condition fails and test is skipped.
  2. 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.
  3. Final Answer:

    The system property is not set before test execution -> Option A
  4. Quick Check:

    Property must be set before test runs [OK]
Hint: Ensure system property is set before running tests [OK]
Common Mistakes:
  • Confusing 'named' with 'name' parameter
  • Assuming regex must be partial match
  • Believing JUnit lacks support for this annotation
5. You want to write a test that runs only if the system property env is set to either dev or test. Which @EnabledIfSystemProperty annotation correctly implements this?
hard
A. @EnabledIfSystemProperty(named = "env", matches = "dev?test")
B. @EnabledIfSystemProperty(named = "env", matches = "dev,test")
C. @EnabledIfSystemProperty(named = "env", matches = "[dev,test]")
D. @EnabledIfSystemProperty(named = "env", matches = "dev|test")

Solution

  1. Step 1: Understand regex for multiple options

    The regex dev|test means match either "dev" or "test" exactly.
  2. 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.
  3. Final Answer:

    @EnabledIfSystemProperty(named = "env", matches = "dev|test") -> Option D
  4. Quick Check:

    Use regex alternation with | for multiple matches [OK]
Hint: Use regex 'dev|test' to match either value [OK]
Common Mistakes:
  • Using commas instead of | in regex
  • Using square brackets which match single chars
  • Misusing ? for alternation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard