What is the purpose of the @EnabledIf annotation in JUnit?
The @EnabledIf annotation allows tests to run only when a custom condition is true. It helps control test execution based on dynamic logic.
Click to reveal answer
beginner
How do you write a simple custom condition method for @EnabledIf?
Create a static method that returns a boolean. The method contains the logic to decide if the test should run. Example:
public static boolean isTestEnabled() { return true; }
Click to reveal answer
intermediate
What must be true about the method used in @EnabledIf's expression?
The method must be static, return a boolean, and be accessible from the test class or a referenced class.
Click to reveal answer
intermediate
How does @EnabledIf improve test suite efficiency?
It skips tests that don't meet certain conditions, saving time and resources by not running irrelevant tests.
Click to reveal answer
beginner
Give an example of using @EnabledIf with a custom condition method.
Example:
@EnabledIf(expression = "com.example.MyConditions.isFeatureEnabled()", reason = "Feature must be enabled")
void testFeature() { /* test code */ }
Click to reveal answer
What type of method can be used with @EnabledIf to control test execution?
AAny method returning String
BA non-static method returning void
CA static method returning boolean
DA static method returning int
✗ Incorrect
The method must be static and return a boolean to be used with @EnabledIf.
What happens if the @EnabledIf condition returns false?
AThe test is skipped
BThe test fails
CThe test runs anyway
DThe test throws an exception
✗ Incorrect
If the condition is false, JUnit skips the test.
Where can the method used in @EnabledIf be located?
AIn the test class or any accessible class
BOnly in the test class
COnly in a superclass
DOnly in a private inner class
✗ Incorrect
The method can be in the test class or any class accessible to the test.
Which of these is a valid reason to use @EnabledIf?
AAutomatically fix test errors
BForce all tests to run
CIgnore test failures
DRun tests only if a feature flag is enabled
✗ Incorrect
@EnabledIf is used to conditionally run tests, such as when a feature flag is enabled.
What is the return type required for a method used in @EnabledIf expression?
Avoid
Bboolean
Cint
DString
✗ Incorrect
The method must return a boolean to indicate if the test should run.
Explain how to create and use a custom condition with @EnabledIf in JUnit.
Think about a method that returns true or false and how you tell JUnit to use it.
You got /4 concepts.
Describe benefits of using @EnabledIf for conditional test execution.
Consider why you might want to run some tests only sometimes.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of using @EnabledIf in JUnit tests?
easy
A. To run a test only if a custom condition returns true
B. To disable a test permanently
C. To mark a test as expected to fail
D. To repeat a test multiple times
Solution
Step 1: Understand the role of @EnabledIf
@EnabledIf is used to conditionally enable a test based on a custom boolean expression or method.
Step 2: Compare with other annotations
Unlike disabling or repeating tests, @EnabledIf controls test execution dynamically based on conditions.
Final Answer:
To run a test only if a custom condition returns true -> Option A
Quick Check:
@EnabledIf runs test if condition true [OK]
Hint: Remember: @EnabledIf controls test run by condition [OK]
Common Mistakes:
Confusing @EnabledIf with @Disabled
Thinking it repeats tests
Assuming it marks tests as expected failures
2. Which of the following is the correct syntax to enable a test only if a static method isFeatureEnabled() returns true using @EnabledIf?
easy
A. @EnabledIf("isFeatureEnabled()")
B. @EnabledIf(expression = "isFeatureEnabled()")
C. @EnabledIf("isFeatureEnabled")
D. @EnabledIf(condition = "isFeatureEnabled()")
Solution
Step 1: Check correct attribute name
The correct attribute for the condition expression is unnamed, so just a string is passed to @EnabledIf.
Step 2: Validate method call syntax
The method must be called with parentheses inside the string: "isFeatureEnabled()".
Final Answer:
@EnabledIf("isFeatureEnabled()") -> Option A
Quick Check:
Use @EnabledIf("method()") syntax [OK]
Hint: Use @EnabledIf("method()") with parentheses [OK]
Common Mistakes:
Using expression attribute which does not exist
Omitting parentheses in method call
Using incorrect attribute name like condition
3. Given the following code snippet, what will be the output when running the test?
public class FeatureTest {
static boolean isFeatureEnabled() { return false; }
@Test
@EnabledIf("isFeatureEnabled()")
void testFeature() {
System.out.println("Feature test executed");
}
}
medium
A. Feature test executed
B. Test skipped, no output
C. Compilation error due to @EnabledIf
D. Runtime exception thrown
Solution
Step 1: Evaluate the condition method
The method isFeatureEnabled() returns false, so the condition for @EnabledIf is false.
Step 2: Understand @EnabledIf behavior
When the condition is false, the test is skipped and the test method body does not run.
Final Answer:
Test skipped, no output -> Option B
Quick Check:
False condition skips test [OK]
Hint: False condition means test does not run [OK]
Common Mistakes:
Expecting test to run despite false condition
Thinking it causes errors
Confusing with @Disabled
4. Identify the error in the following test code using @EnabledIf:
public class SampleTest {
@Test
@EnabledIf("checkCondition")
void testSomething() {}
boolean checkCondition() { return true; }
}
medium
A. checkCondition must return String, not boolean
B. @EnabledIf requires expression attribute
C. checkCondition method must be static
D. Method checkCondition must be public
Solution
Step 1: Check method signature requirements
The method used in @EnabledIf must be static because JUnit calls it without instance.
Step 2: Verify method return type and visibility
Return type boolean is correct and method visibility can be default or public, but static is mandatory.
Final Answer:
checkCondition method must be static -> Option C
Quick Check:
Condition method must be static [OK]
Hint: Condition methods must be static [OK]
Common Mistakes:
Making condition method non-static
Expecting expression attribute in @EnabledIf
Confusing return type requirements
5. You want to enable a test only if the system property env equals prod and a static method isReady() returns true. Which @EnabledIf expression correctly combines these conditions?
hard
A. @EnabledIf(expression = "System.getProperty('env').equals('prod') && isReady()")
B. @EnabledIf("System.getProperty('env') == 'prod' && isReady()")
C. @EnabledIf("System.getProperty('env').equals('prod') & isReady()")
D. @EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()")
Solution
Step 1: Understand string comparison in Java expressions
Use 'prod'.equals(System.getProperty('env')) to avoid NullPointerException if property is null.
Step 2: Combine conditions with logical AND
Use && to combine boolean expressions correctly in the string.
Step 3: Validate syntax of @EnabledIf
The expression is passed as a single string without attribute name; @EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()") matches this.
Final Answer:
@EnabledIf("'prod'.equals(System.getProperty('env')) && isReady()") -> Option D
Quick Check:
Use safe equals and && in string expression [OK]
Hint: Use 'prod'.equals(...) and && for safe combined condition [OK]
Common Mistakes:
Using == for string comparison
Using single & instead of &&
Adding expression attribute which is invalid
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.