0
0
JUnittesting~20 mins

@EnabledIfSystemProperty in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.