@EnabledIf is a JUnit Jupiter condition annotation that runs a test only when a method you name returns true. Since JUnit 5.7 it lives in org.junit.jupiter.api.condition and takes the name of a condition method, so you can enable or skip tests based on any logic you can write in Java, such as environment checks, feature flags or available services. Its counterpart is @DisabledIf.
Do not confuse it with the Spring TestContext annotation of the same name, org.springframework.test.context.junit.jupiter.EnabledIf, which evaluates a SpEL expression instead. Both are covered below.
@EnabledIf does in JUnit Jupiter@EnabledIf with SpEL@EnabledIf does in JUnit Jupiter@EnabledIf is a conditional test execution annotation. Before a test method or test class runs, the Jupiter engine evaluates the condition. If it is false, the test is reported as skipped (aborted before execution) rather than failed. You can place it on a test method or on a test class, where it applies to every test inside.
The annotation has two attributes:
| Attribute | Purpose |
|---|---|
value | Name of the condition method, or a fully qualified package.Class#method reference |
disabledReason | Text shown in the report when the test is skipped (since 5.7) |
A condition method must return boolean and take either no parameters or a single ExtensionContext parameter. When the method sits in the test class, it must be static, unless the test class uses @TestInstance(Lifecycle.PER_CLASS), in which case it can be an instance method.
When the variable is missing, the report lists chargesTestCard as skipped with the reason you supplied. That reason is what makes a skipped test understandable in CI output, so always set it.
When several test classes share one condition, put the method in a helper class and reference it with a fully qualified name and a # separator. External condition methods must be static.
A condition method can also accept an ExtensionContext, which lets you read configuration parameters or the current test class:
Before writing a custom method, check whether Jupiter already ships the condition. These are simpler and need no helper code.
| Annotation | Enables the test when |
|---|---|
@EnabledOnOs | The operating system matches |
@EnabledOnJre / @EnabledForJreRange | The Java runtime version matches |
@EnabledIfSystemProperty | A system property matches a regular expression |
@EnabledIfEnvironmentVariable | An environment variable matches a regular expression |
@EnabledIf | Your own method returns true |
Each has a @Disabled... counterpart, for example @DisabledOnOs and @DisabledIf. Use @EnabledIf when the logic needs more than a simple match, such as combining two variables or probing a network port.
@EnabledIf with SpELSpring Framework ships its own @EnabledIf and @DisabledIf in org.springframework.test.context.junit.jupiter. They take a SpEL expression, can read the Spring Environment, and expose a loadContext flag that controls whether the ApplicationContext is loaded before evaluating the expression.
Use the Spring version only when you need the Environment or other Spring state. For plain Java conditions, the Jupiter annotation is clearer, has no SpEL string to mistype and works without Spring on the classpath.
PER_METHOD lifecycle. Jupiter cannot call it before an instance exists and fails with a clear exception.@EnabledIf. The IDE may offer both the Jupiter and the Spring one, and they take different arguments.disabledReason, which leaves a skipped test with no explanation in CI.@EnabledIf?The Jupiter @EnabledIf and @DisabledIf with a method name were added in JUnit 5.7. In earlier releases only the Spring SpEL-based version existed.
@EnabledIf on a class?Yes. When placed on a test class, the condition is evaluated once for that class and all tests are skipped if it returns false.
Write a single condition method that combines them with && or ||. Alternatively, stack several condition annotations; a test runs only if every condition passes.
A skipped test was never executed because a condition was not met, and it does not break the build. A failed test ran and an assertion or exception failed.
@EnabledIf lets a JUnit 5 test run only when a boolean method says so. Use disabledReason for readable reports, make the method static unless you use PER_CLASS, reach for built-in conditions such as @EnabledOnOs first, and remember that Spring's SpEL-based @EnabledIf is a different annotation with a different package.