0
0
JUnittesting~20 mins

@EnabledOnJre for JRE-specific tests in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JRE Conditional Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a test annotated with @EnabledOnJre for Java 17
Consider this JUnit 5 test class snippet. What will be the test execution result if run on Java 17?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;

public class SampleTest {

    @Test
    @EnabledOnJre(JRE.JAVA_17)
    void testOnlyOnJava17() {
        System.out.println("Running on Java 17");
    }

    @Test
    void alwaysRun() {
        System.out.println("Always runs");
    }
}
ABoth tests run and print their messages.
BOnly the test annotated with @EnabledOnJre runs and prints its message.
COnly the test without @EnabledOnJre runs and prints its message.
DNeither test runs because @EnabledOnJre disables all tests.
Attempts:
2 left
💡 Hint
Think about what @EnabledOnJre does and which tests have it.
assertion
intermediate
2:00remaining
Correct assertion to verify @EnabledOnJre disables test on Java 11
You want to assert that a test annotated with @EnabledOnJre(JRE.JAVA_17) does NOT run on Java 11. Which assertion correctly verifies this behavior in a test suite?
AassertFalse(testExecuted, "Test should not run on Java 11");
BassertTrue(testExecuted, "Test should run on Java 11");
CassertEquals("Running", testOutput);
DassertNull(testExecuted);
Attempts:
2 left
💡 Hint
If the test is disabled, it should not have executed.
🔧 Debug
advanced
2:00remaining
Why does @EnabledOnJre not disable test on Java 18?
Given this test annotation: @EnabledOnJre(JRE.JAVA_17), why does the test still run on Java 18?
ABecause the annotation is ignored on Java versions higher than 17.
BBecause @EnabledOnJre matches the specified JRE and all later versions, so Java 18 is included and test runs.
CBecause @EnabledOnJre matches only the exact JRE version specified, so Java 18 is not matched and test is disabled.
DBecause @EnabledOnJre only disables tests on earlier versions, not later ones.
Attempts:
2 left
💡 Hint
Check how @EnabledOnJre matches JRE versions exactly.
🧠 Conceptual
advanced
2:00remaining
Purpose of @EnabledOnJre in test suites
What is the main purpose of using @EnabledOnJre in JUnit test suites?
ATo automatically upgrade Java version before running tests.
BTo disable tests permanently regardless of Java version.
CTo run tests only on operating systems matching the JRE version.
DTo run tests only on specific Java Runtime Environment versions.
Attempts:
2 left
💡 Hint
Think about controlling test execution based on Java version.
framework
expert
2:00remaining
Combining @EnabledOnJre with other conditional annotations
You want a test to run only on Java 17 and only on Windows OS. Which annotation combination correctly achieves this in JUnit 5?
A@EnabledOnJre(JRE.JAVA_17, OS.WINDOWS)
B@EnabledOnJre(JRE.JAVA_17) @EnabledOnOs(OS.WINDOWS)
C@EnabledOnOs(OS.WINDOWS) @EnabledOnJre(OS.WINDOWS, JRE.JAVA_17)
D@EnabledOnJre(OS.WINDOWS) @EnabledOnOs(JRE.JAVA_17)
Attempts:
2 left
💡 Hint
Use separate annotations for JRE and OS conditions.