0
0
JUnittesting~5 mins

@EnabledIfEnvironmentVariable in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
Which attribute specifies the environment variable name in @EnabledIfEnvironmentVariable?
Anamed
Bvalue
Cvariable
DenvName
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
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
Which of these is a valid use of @EnabledIfEnvironmentVariable?
A@EnabledIfEnvironmentVariable(env = "MODE", value = "production")
B@EnabledIfEnvironmentVariable(variable = "MODE", equals = "production")
C@EnabledIfEnvironmentVariable(named = "MODE", matches = "production")
D@EnabledIfEnvironmentVariable(name = "MODE", match = "production")
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.