Test Overview
This test checks if a method runs only on a specific Java Runtime Environment (JRE) version using the @EnabledOnJre annotation. It verifies that the test executes only when the JRE matches the specified version.
This test checks if a method runs only on a specific Java Runtime Environment (JRE) version using the @EnabledOnJre annotation. It verifies that the test executes only when the JRE matches the specified version.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnJre; import org.junit.jupiter.api.condition.JRE; import static org.junit.jupiter.api.Assertions.assertTrue; public class JreSpecificTest { @Test @EnabledOnJre(JRE.JAVA_17) void testRunsOnlyOnJava17() { String version = System.getProperty("java.version"); assertTrue(version.startsWith("17"), "Test should run only on Java 17"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts and detects the test method annotated with @EnabledOnJre(JRE.JAVA_17) | JUnit test environment initialized, Java version detected from system properties | — | PASS |
| 2 | JUnit checks current JRE version; if it is Java 17, proceeds to run the test method | Running on Java 17 environment | — | PASS |
| 3 | Test method executes and retrieves the Java version string from system properties | Inside test method, Java version string obtained | — | PASS |
| 4 | Assertion checks if the Java version string starts with "17" | Java version string is "17.x.x" | assertTrue(version.startsWith("17")) | PASS |
| 5 | Test completes successfully because the assertion passed | Test method finished without exceptions | — | PASS |
@EnabledOnJre annotation in JUnit?@EnabledOnJre?@EnabledOnJre(JRE.JAVA_17).@EnabledOnJre({JRE.JAVA_11, JRE.JAVA_17}), what happens when running on Java 15?@EnabledOnJre(JRE.JAVA_8, JRE.JAVA_11)@EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17}). @EnabledOnJre(JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17) misses braces, C uses strings instead of enums, D uses a wrong attribute name.Pass the test and this pattern moves unknown