0
0
JUnittesting~20 mins

Custom conditions with @EnabledIf in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit @EnabledIf Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a test with @EnabledIf condition
Consider the following JUnit 5 test method using @EnabledIf. What will be the test execution result when the system property 'env' is set to 'prod'?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

public class EnvTest {

    @Test
    @EnabledIf(expression = "'prod'.equals(System.getProperty('env'))", reason = "Only runs in prod environment")
    void testOnlyInProd() {
        System.out.println("Running in prod");
    }
}
ATest is skipped and does not run
BTest runs and passes, printing 'Running in prod'
CTest runs but fails with an exception
DTest runs and passes but prints nothing
Attempts:
2 left
💡 Hint
Check what the @EnabledIf expression evaluates to when 'env' is 'prod'.
assertion
intermediate
2:00remaining
Assertion to verify @EnabledIf disables test
You want to assert that a test annotated with @EnabledIf(expression = "false") does not run. Which assertion correctly verifies this behavior in a JUnit 5 test suite?
AassertThrows(TestAbortedException.class, () -> runTest())
BassertTrue(runTest())
CassertFalse(runTest())
DassertEquals("Skipped", runTest())
Attempts:
2 left
💡 Hint
Tests disabled by @EnabledIf are aborted, not failed or passed.
🔧 Debug
advanced
3:00remaining
Debug why @EnabledIf expression fails to enable test
Given this test method with @EnabledIf: @Test @EnabledIf(expression = "systemProperties['user.name'] == 'admin'", reason = "Only admin user") void adminOnlyTest() {} The test never runs even when the system property 'user.name' is set to 'admin'. What is the likely cause?
AThe expression uses '==' instead of '.equals()' for string comparison
BThe system property 'user.name' is not accessible in the test environment
CThe @EnabledIf annotation requires a static method, missing here
DThe test method must be static to use @EnabledIf
Attempts:
2 left
💡 Hint
Remember how string equality works in Java expressions.
🧠 Conceptual
advanced
2:00remaining
Understanding @EnabledIf expression context
Which of the following statements about the expression used in @EnabledIf is TRUE?
AThe expression can only check JUnit test annotations, not system properties
BThe expression can execute arbitrary Java code including method calls with side effects
CThe expression must be a constant boolean literal (true or false) only
DThe expression can access system properties and environment variables via predefined variables
Attempts:
2 left
💡 Hint
Think about what data the expression can read to decide test execution.
framework
expert
4:00remaining
Custom condition class for @EnabledIf usage
You want to create a custom condition class to use with @EnabledIf to enable tests only on weekdays. Which implementation correctly defines the condition method?
JUnit
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.time.DayOfWeek;
import java.time.LocalDate;

public class WeekdayCondition implements ExecutionCondition {

    @Override
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
        DayOfWeek day = LocalDate.now().getDayOfWeek();
        if (day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY) {
            return ConditionEvaluationResult.disabled("Weekend - test disabled");
        } else {
            return ConditionEvaluationResult.enabled("Weekday - test enabled");
        }
    }
}
AThe class extends ConditionEvaluationResult and overrides isEnabled to check the day
BThe class implements TestExecutionListener and overrides beforeTestExecution to skip tests on weekends
CThe class implements ExecutionCondition and overrides evaluateExecutionCondition returning enabled or disabled based on day
DThe class implements ExecutionCondition but returns null from evaluateExecutionCondition
Attempts:
2 left
💡 Hint
Check the interface and method required for custom conditions in JUnit 5.