Test Execution Conditions

Let's learn how to test execution conditions in JUnit 5.

We'll cover the following

Multiple annotations

We discussed the @Disabled annotation to disable test classes or methods. It’s actually implemented as a built-in extension of ExecutionCondition. This ExecutionCondition can be evaluated before the execution of each test class or test method to determine if the test class or method should be executed. It has only one method, ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context), at line 10 in the code below.

The return value of type ConditionEvaluationResult contains a boolean value to indicate if the test is disabled and an optional message about the reason to enable or disable it. The ConditionEvaluationResult objects are usually created using static methods enabled(String reason) and are disabled (String reason). The implementation of the @Disabled annotation is the org.junit.jupiter.engine.extension.DisabledCondition class, which checks for the existence of @Disabled.

Get hands-on with 1400+ tech skills courses.