0
0
JUnittesting~5 mins

Custom conditions with @EnabledIf in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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: <br>
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:<br>
@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
What happens if the @EnabledIf condition returns false?
AThe test is skipped
BThe test fails
CThe test runs anyway
DThe test throws an exception
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
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
What is the return type required for a method used in @EnabledIf expression?
Avoid
Bboolean
Cint
DString
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.