Close

JUnit 5 - Enable/Disable with @EnabledIf and @DisabledIf

[Last Updated: Dec 5, 2025]

JUnit Jupiter provides @EnabledIf and @DisabledIf to control execution dynamically, based on a boolean-returning method referenced by name. If the referenced method returns true, @EnabledIf will run the test and @DisabledIf will skip it.

Overview

  • Reference a method by name within the same class (or via a fully-qualified name).
  • The method must return boolean or Boolean; it can be instance or static.
  • Helpful for complex or reusable conditions that don’t fit property/env checks.
  • Keep condition methods fast and deterministic whenever possible.

Quick syntax

@org.junit.jupiter.api.condition.EnabledIf("isFeatureEnabled")
void runsWhenTrue() { /* ... */ }

@org.junit.jupiter.api.condition.DisabledIf("isFeatureEnabled")
void skippedWhenTrue() { /* ... */ }

boolean isFeatureEnable(){
  ......
}

Java source and doc

Definition of EnabledIf

(Version: junit-5 6.0.1)
package org.junit.jupiter.api.condition;
   ........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnabledIfCondition.class)
@API(status = STABLE, since = "5.7")
@SuppressWarnings("exports")
public @interface EnabledIf {
    String value(); 1
    String disabledReason() default ""; 2
}
1 The name of a method within the test class or in an external class to use as a condition for the test's or container's execution.

Condition methods must be static if located outside the test class or if @EnabledIf is used at the class level.

A condition method in an external class must be referenced by its fully qualified method name — for example, com.example.Conditions#isEncryptionSupported.

2 Custom reason to provide if the test or container is disabled.

If a custom reason is supplied, it will be combined with the default reason for this annotation. If a custom reason is not supplied, the default reason will be used.

Definition of DisabledIf

(Version: junit-5 6.0.1)
package org.junit.jupiter.api.condition;
   ........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(DisabledIfCondition.class)
@API(status = STABLE, since = "5.7")
@SuppressWarnings("exports")
public @interface DisabledIf {
    String value(); 1
    String disabledReason() default ""; 2
}
1 The name of a method within the test class or in an external class to use as a condition for the test's or container's execution.

Condition methods must be static if located outside the test class or if @DisabledIf is used at the class level.

A condition method in an external class must be referenced by its fully qualified method name — for example, com.example.Conditions#isEncryptionSupported.

2 Custom reason to provide if the test or container is disabled.

If a custom reason is supplied, it will be combined with the default reason for this annotation. If a custom reason is not supplied, the default reason will be used.

Example

Using @EnabledIf and @DisabledIf

package com.logicbig.example;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIf;
import org.junit.jupiter.api.condition.EnabledIf;

import static org.junit.jupiter.api.Assertions.assertTrue;

class EnabledDisabledIfExamplesTest {

    // --- Examples using a simple boolean supplier method ---

    @Test
    @EnabledIf("alwaysTrue")
    void runs_when_condition_is_true() {
        assertTrue(true);
    }

    @Test
    @DisabledIf("alwaysTrue")
    void skipped_when_condition_is_true() {
        assertTrue(true);
    }

    // --- Example using a method that checks an arbitrary JVM detail ---

    @Test
    @EnabledIf("isCurrentYearAtLeast2020")
    void runs_when_current_year_is_recent() {
        assertTrue(true);
    }

    // Condition methods referenced by name above. They can be instance or static methods.
    boolean alwaysTrue() {
        return true;
    }

    static boolean isCurrentYearAtLeast2020() {
        java.time.Year y = java.time.Year.now();
        return y.getValue() >= 2020;
    }
}
mvn test -Dtest=EnabledDisabledIfExamplesTest

Output

D:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-enabled-disabled-if-annotation>mvn test -Dtest=EnabledDisabledIfExamplesTest
[INFO] Scanning for projects...
[INFO]
[INFO] ----< com.logicbig.example:junit-5-enabled-disabled-if-annotation >-----
[INFO] Building junit-5-enabled-disabled-if-annotation 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-enabled-disabled-if-annotation ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-enabled-disabled-if-annotation\src\main\resources
[INFO]
[INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-enabled-disabled-if-annotation ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-enabled-disabled-if-annotation ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-enabled-disabled-if-annotation\src\test\resources
[INFO]
[INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-enabled-disabled-if-annotation ---
[INFO] Recompiling the module because of added or removed source files.
[INFO] Compiling 1 source file with javac [debug target 25] to target\test-classes
[INFO]
[INFO] --- surefire:3.5.0:test (default-test) @ junit-5-enabled-disabled-if-annotation ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] +--com.logicbig.example.EnabledDisabledIfExamplesTest - 0.083 ss
[INFO] | +-- [??] skipped_when_condition_is_true (@DisabledIf("alwaysTrue") evaluated to true) - 0 ss
[INFO] | +-- [OK] runs_when_current_year_is_recent - 0.028 ss
[INFO] | '-- [OK] runs_when_condition_is_true - 0.010 ss
[INFO]
[INFO] Results:
[INFO]
[WARNING] Tests run: 3, Failures: 0, Errors: 0, Skipped: 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.927 s
[INFO] Finished at: 2025-12-04T05:21:22+08:00
[INFO] ------------------------------------------------------------------------

Running in IntelliJ


Example Project

Dependencies and Technologies Used:

  • junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
     Version Compatibility: 5.7.0 - 6.0.1Version List
    ×

    Version compatibilities of junit-jupiter-engine with this example:

    • 5.7.0
    • 5.7.1
    • 5.7.2
    • 5.8.0
    • 5.8.1
    • 5.8.2
    • 5.9.0
    • 5.9.1
    • 5.9.2
    • 5.9.3
    • 5.10.0
    • 5.10.1
    • 5.10.2
    • 5.10.3
    • 5.10.4
    • 5.10.5
    • 5.11.0
    • 5.11.1
    • 5.11.2
    • 5.11.3
    • 5.11.4
    • 5.12.0
    • 5.12.1
    • 5.12.2
    • 5.13.0
    • 5.13.1
    • 5.13.2
    • 5.13.3
    • 5.13.4
    • 5.14.0
    • 5.14.1
    • 6.0.0
    • 6.0.1

    Versions in green have been tested.

  • JDK 25
  • Maven 3.9.11

JUnit 5 - Enabled Disabled If Annotation Select All Download
  • junit-5-enabled-disabled-if-annotation
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • EnabledDisabledIfExamplesTest.java

    See Also