0
0
JUnittesting~10 mins

@EnabledOnJre for JRE-specific tests in JUnit - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - JUnit 5
JUnit
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");
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test runner starts and detects the test method annotated with @EnabledOnJre(JRE.JAVA_17)JUnit test environment initialized, Java version detected from system properties-PASS
2JUnit checks current JRE version; if it is Java 17, proceeds to run the test methodRunning on Java 17 environment-PASS
3Test method executes and retrieves the Java version string from system propertiesInside test method, Java version string obtained-PASS
4Assertion checks if the Java version string starts with "17"Java version string is "17.x.x"assertTrue(version.startsWith("17"))PASS
5Test completes successfully because the assertion passedTest method finished without exceptions-PASS
Failure Scenario
Failing Condition: The test runs on a JRE version other than Java 17
Execution Trace Quiz - 3 Questions
Test your understanding
What does the @EnabledOnJre(JRE.JAVA_17) annotation do in this test?
ARuns the test only if the JRE is Java 17
BRuns the test on all JRE versions except Java 17
CDisables the test permanently
DRuns the test only on Java 8
Key Result
Use @EnabledOnJre to run tests only on specific Java versions, ensuring compatibility and avoiding false failures on unsupported JREs.