JUnit Jupiter can conditionally run tests based on the Java runtime (JRE/JDK) version via @EnabledOnJre, DisabledOnJre, @EnabledForJreRange and @DisabledForJreRange. This is useful when features or APIs differ across Java releases.
Definition of EnabledOnJre(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnabledOnJreCondition.class)
@API(status = STABLE, since = "5.1")
@SuppressWarnings("exports")
public @interface EnabledOnJre {
JRE[] value() default {};
@API(status = MAINTAINED, since = "5.13.3")
int[] versions() default {};
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Definition of DisabledOnJre(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(DisabledOnJreCondition.class)
@API(status = STABLE, since = "5.1")
@SuppressWarnings("exports")
public @interface DisabledOnJre {
JRE[] value() default {};
@API(status = MAINTAINED, since = "5.13.3")
int[] versions() default {};
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Definition of EnabledForJreRange(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(EnabledForJreRangeCondition.class)
@API(status = STABLE, since = "5.6")
@SuppressWarnings("exports")
public @interface EnabledForJreRange {
JRE min() default JRE.UNDEFINED;
JRE max() default JRE.UNDEFINED;
@API(status = MAINTAINED, since = "5.13.3")
int minVersion() default -1;
@API(status = MAINTAINED, since = "5.13.3")
int maxVersion() default -1;
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Definition of DisabledForJreRange(Version: junit-5 6.0.1) package org.junit.jupiter.api.condition;
........
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(DisabledForJreRangeCondition.class)
@API(status = STABLE, since = "5.6")
@SuppressWarnings("exports")
public @interface DisabledForJreRange {
JRE min() default JRE.UNDEFINED;
JRE max() default JRE.UNDEFINED;
@API(status = MAINTAINED, since = "5.13.3")
int minVersion() default -1;
@API(status = MAINTAINED, since = "5.13.3")
int maxVersion() default -1;
@API(status = STABLE, since = "5.7")
String disabledReason() default "";
}
Overview
@EnabledOnJre / @DisabledOnJre target specific versions.
@EnabledForJreRange / @DisabledForJreRange target version ranges.
- Version constants are available in
org.junit.jupiter.api.condition.JRE.
Quick syntax
@EnabledForJreRange(min = JRE.JAVA_9)
void test() { /* ... */ }
@EnabledOnJre(JRE.JAVA_21)
void onlyOn21() { /* ... */ }
@DisabledOnJre(JRE.JAVA_8)
void notOn8() { /* ... */ }
Example
JRE-based conditions
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
class JreConditionExamplesTest {
@Test
@EnabledForJreRange(max = JRE.JAVA_17, disabledReason = "enable if max Java 17 and older")
void runs_on_java17_and_older() {
assertTrue(true);
}
@Test
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_17, disabledReason = "diable between Java 8 and Java 17")
void disabled_on_java8_to_java17() {
assertTrue(true);
}
@Test
@EnabledOnJre(value = JRE.JAVA_17, disabledReason = "enable if Java 17")
void runs_only_on_java17() {
assertTrue(true);
}
@Test
@DisabledOnJre(JRE.JAVA_8)
void not_run_on_java8() {
assertTrue(true);
}
@Test
void always_runs_control() {
assertTrue(true);
}
}
mvn test -Dtest=JreConditionExamplesTest OutputD:\example-projects\junit-5\junit-5-enabling-disabling-tests\junit-5-enabled-disabled-for-jre>mvn test -Dtest=JreConditionExamplesTest [INFO] Scanning for projects... [INFO] [INFO] -------< com.logicbig.example:junit-5-enabled-disabled-for-jre >-------- [INFO] Building junit-5-enabled-disabled-for-jre 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-enabled-disabled-for-jre --- [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-for-jre\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-enabled-disabled-for-jre --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-enabled-disabled-for-jre --- [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-for-jre\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-enabled-disabled-for-jre --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.5.0:test (default-test) @ junit-5-enabled-disabled-for-jre --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] +--com.logicbig.example.JreConditionExamplesTest - 0.085 ss [INFO] | +-- [??] disabled_on_java21_and_newer (Disabled on JRE version: 25 ==> diable if min Java 21) - 0 ss [INFO] | +-- [OK] not_run_on_java8 - 0.032 ss [INFO] | +-- [OK] runs_only_on_java25 - 0 ss [INFO] | +-- [OK] always_runs_control - 0 ss [INFO] | +-- [OK] runs_on_java25_and_older - 0 ss [INFO] | '-- [OK] disabled_on_java17_to_java25 - 0 ss [INFO] [INFO] Results: [INFO] [WARNING] Tests run: 6, Failures: 0, Errors: 0, Skipped: 1 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.817 s [INFO] Finished at: 2025-12-04T07:09:30+08:00 [INFO] ------------------------------------------------------------------------
Running in IntelliJ

Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.7.1 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 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
|