What is the purpose of the @EnabledIfEnvironmentVariable annotation in JUnit?
It allows a test or test class to run only if a specific environment variable matches a given value. This helps control test execution based on environment settings.
Click to reveal answer
beginner
How do you specify the environment variable name and value in @EnabledIfEnvironmentVariable?
You use the 'named' attribute for the environment variable name and the 'matches' attribute for the expected value pattern. For example: @EnabledIfEnvironmentVariable(named = "ENV_VAR", matches = "true")
Click to reveal answer
beginner
Can @EnabledIfEnvironmentVariable be used on both test methods and test classes?
Yes, it can be applied to individual test methods or entire test classes to conditionally enable or disable them based on environment variables.
Click to reveal answer
beginner
What happens if the environment variable does not match the specified value in @EnabledIfEnvironmentVariable?
The test or test class is skipped and not executed. This helps avoid running tests in unsuitable environments.
Click to reveal answer
intermediate
Give a real-life example where @EnabledIfEnvironmentVariable is useful.
When you have tests that should only run in a development environment, you can set an environment variable like 'ENV' to 'dev' and use @EnabledIfEnvironmentVariable(named = "ENV", matches = "dev") to run those tests only in development.
Click to reveal answer
What does @EnabledIfEnvironmentVariable do in JUnit?
ARuns tests only if a specific environment variable matches a value
BDisables tests permanently
CRuns tests only on weekends
DEnables tests based on system properties
✗ Incorrect
The annotation enables tests conditionally based on environment variable values.
Which attribute specifies the environment variable name in @EnabledIfEnvironmentVariable?
Anamed
Bvalue
Cvariable
DenvName
✗ Incorrect
The 'named' attribute is used to specify the environment variable name.
If the environment variable does not match the expected value, what happens to the test?
AIt throws an error
BIt fails
CIt runs anyway
DIt is skipped
✗ Incorrect
Tests are skipped if the environment variable does not match.
Can @EnabledIfEnvironmentVariable be applied to a test class?
ANo, only test methods
BYes, both test classes and methods
COnly to constructors
DOnly to static methods
✗ Incorrect
It can be applied to both test classes and test methods.
Which of these is a valid use of @EnabledIfEnvironmentVariable?
A@EnabledIfEnvironmentVariable(env = "MODE", value = "production")
D@EnabledIfEnvironmentVariable(name = "MODE", match = "production")
✗ Incorrect
The correct attributes are 'named' and 'matches'.
Explain how @EnabledIfEnvironmentVariable controls test execution in JUnit.
Think about how environment settings can decide if a test runs.
You got /4 concepts.
Describe a scenario where using @EnabledIfEnvironmentVariable would be helpful.
Consider when some tests should not run everywhere.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the @EnabledIfEnvironmentVariable annotation in JUnit?
easy
A. To set environment variables before running tests
B. To disable all tests in a test class
C. To run a test only if a specific environment variable has a certain value
D. To run tests in parallel threads
Solution
Step 1: Understand the annotation's role
<code>@EnabledIfEnvironmentVariable
Step 2: Identify its effect on tests
It enables tests only when the environment variable matches the given value, skipping otherwise.
Final Answer:
To run a test only if a specific environment variable has a certain value -> Option C
Quick Check:
Runs test if env var matches = A [OK]
Hint: Think: runs test only if env var matches value [OK]
Common Mistakes:
Confusing it with disabling tests
Assuming it sets environment variables
Thinking it controls test parallelism
2. Which of the following is the correct syntax to enable a test only when the environment variable ENV equals prod using @EnabledIfEnvironmentVariable?
easy
A. @EnabledIfEnvironmentVariable(named = "ENV", matches = "prod")
B. @EnabledIfEnvironmentVariable(name = "ENV", value = "prod")
C. @EnabledIfEnvironmentVariable(variable = "ENV", equals = "prod")
D. @EnabledIfEnvironmentVariable(env = "ENV", match = "prod")
Solution
Step 1: Recall correct attribute names
The annotation uses named for the environment variable name and matches for the expected value.
Step 2: Match syntax with options
@EnabledIfEnvironmentVariable(named = "ENV", matches = "prod") correctly uses named = "ENV" and matches = "prod".
Final Answer:
@EnabledIfEnvironmentVariable(named = "ENV", matches = "prod") -> Option A
Quick Check:
Correct attribute names = A [OK]
Hint: Remember: named and matches are correct attributes [OK]
Common Mistakes:
Using incorrect attribute names like name or value
Mixing attribute names with variable or env
Using equals or match instead of matches
3. Given the following test code snippet, what will happen if the environment variable MODE is set to test?
B. The test will run and print "Running in prod mode"
C. The test will fail with an exception
D. The test will run but print nothing
Solution
Step 1: Check environment variable value
The environment variable MODE is set to test, but the annotation requires prod.
Step 2: Understand annotation behavior
Since the value does not match, the test is skipped and does not run.
Final Answer:
The test will be skipped and not run -> Option A
Quick Check:
Env var mismatch skips test = B [OK]
Hint: If env var value differs, test is skipped [OK]
Common Mistakes:
Assuming test runs regardless of env var
Expecting an exception on mismatch
Thinking test runs but prints nothing
4. Identify the error in the following JUnit test code using @EnabledIfEnvironmentVariable:
@EnabledIfEnvironmentVariable(named = "APP_ENV", match = "dev")
void testInDev() {
// test code
}
medium
A. The annotation must be placed on the class, not the method
B. The attribute name should be 'matches' instead of 'match'
C. The method must be static
D. The environment variable name cannot be 'APP_ENV'
Solution
Step 1: Check attribute names in annotation
The correct attribute for matching value is matches, not match.
Step 2: Verify other code aspects
Environment variable name can be any string, method need not be static, and annotation can be on method.
Final Answer:
The attribute name should be 'matches' instead of 'match' -> Option B
Quick Check:
Correct attribute is matches, not match [OK]
Hint: Use 'matches' attribute, not 'match' [OK]
Common Mistakes:
Using 'match' instead of 'matches'
Thinking method must be static
Believing annotation only works on class
5. You want to run a test only if the environment variable DEPLOY_ENV is set to either staging or production. How can you achieve this using @EnabledIfEnvironmentVariable in JUnit?
hard
A. Use @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "staging,production") with comma-separated values
B. Use two test methods each annotated with @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "staging") and @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "production") respectively
C. Use @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "staging") and add an OR condition inside the test method
D. Use @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "staging|production") with regex matching
Solution
Step 1: Understand annotation supports regex
The matches attribute supports regular expressions to match multiple values.
Step 2: Use regex for multiple values
Using staging|production matches either value in one annotation.
Final Answer:
Use @EnabledIfEnvironmentVariable(named = "DEPLOY_ENV", matches = "staging|production") with regex matching -> Option D
Quick Check:
Regex in matches handles multiple values = B [OK]
Hint: Use regex in matches to cover multiple values [OK]
Common Mistakes:
Trying to list multiple values with commas
Splitting into multiple test methods unnecessarily
Adding logic inside test instead of annotation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.