0
0
JUnittesting~20 mins

@EnabledIfEnvironmentVariable in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding @EnabledIfEnvironmentVariable Usage
What is the primary purpose of the JUnit annotation @EnabledIfEnvironmentVariable?
ATo enable a test only when a system property is set
BTo disable a test permanently regardless of environment variables
CTo run a test only if a specific environment variable matches a given value
DTo run a test only if the JVM version is above a certain number
Attempts:
2 left
💡 Hint
Think about controlling test execution based on environment settings.
Predict Output
intermediate
2:00remaining
Test Execution Result with @EnabledIfEnvironmentVariable
Given the following test code, what will be the test execution result if the environment variable TEST_MODE is set to production?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

public class EnvVarTest {

    @Test
    @EnabledIfEnvironmentVariable(named = "TEST_MODE", matches = "development")
    void testOnlyInDevelopment() {
        System.out.println("Running test in development mode");
    }
}
ATest will run but not print anything
BTest will be skipped (disabled) because TEST_MODE is 'production'
CTest will fail with an exception due to environment variable mismatch
DTest will run and print the message
Attempts:
2 left
💡 Hint
Check the value the annotation expects versus the actual environment variable.
assertion
advanced
2:30remaining
Correct Assertion for Environment Variable Check
Which assertion correctly verifies that a test method annotated with @EnabledIfEnvironmentVariable(named = "MODE", matches = "test") runs only when the environment variable MODE is set to test?
AassertTrue(System.getenv("MODE").equals("test"))
BassertEquals("test", System.getProperty("MODE"))
CassertNotNull(System.getenv("MODE"))
DassertFalse(System.getenv("MODE").isEmpty())
Attempts:
2 left
💡 Hint
Remember environment variables are accessed differently than system properties.
🔧 Debug
advanced
3:00remaining
Debugging @EnabledIfEnvironmentVariable Not Working
A developer writes a test with @EnabledIfEnvironmentVariable(named = "APP_ENV", matches = "staging") but the test is skipped even when APP_ENV is set to staging. What is the most likely cause?
AThe environment variable <code>APP_ENV</code> is not set in the test runtime environment
BThe annotation <code>@EnabledIfEnvironmentVariable</code> does not support the <code>matches</code> attribute
CThe test method is missing the <code>@Test</code> annotation
DJUnit version does not support conditional annotations
Attempts:
2 left
💡 Hint
Consider how environment variables are passed to the test process.
framework
expert
3:00remaining
Combining @EnabledIfEnvironmentVariable with Other Conditions
Consider a test annotated with both @EnabledIfEnvironmentVariable(named = "REGION", matches = "US") and @EnabledIfEnvironmentVariable(named = "VERSION", matches = "1.0"). Under which condition will the test run?
AThe test will never run because multiple @EnabledIfEnvironmentVariable annotations are not supported
BIf either REGION equals 'US' or VERSION equals '1.0'
COnly if REGION equals 'US', VERSION is ignored
DOnly if both REGION equals 'US' and VERSION equals '1.0'
Attempts:
2 left
💡 Hint
Think about how multiple conditional annotations combine in JUnit.