Bird
Raised Fist0
JUnittesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @EnabledOnJre annotation in JUnit?
easy
A. To run a test only on specified Java Runtime Environment versions
B. To disable a test permanently
C. To run a test on all Java versions except specified ones
D. To enable tests only on the latest Java version

Solution

  1. Step 1: Understand the annotation purpose

    <code>@EnabledOnJre
  2. Step 2: Match purpose with options

    To run a test only on specified Java Runtime Environment versions correctly states it runs tests only on specified JRE versions, while others describe different behaviors.
  3. Final Answer:

    To run a test only on specified Java Runtime Environment versions -> Option A
  4. Quick Check:

    @EnabledOnJre = run test on chosen JRE versions [OK]
Hint: Think: runs tests only on chosen Java versions [OK]
Common Mistakes:
  • Confusing @EnabledOnJre with disabling tests
  • Assuming it runs tests on all versions except specified
  • Thinking it only enables tests on the latest Java version
2. Which of the following is the correct syntax to enable a test only on Java 17 using @EnabledOnJre?
easy
A. @EnabledOnJre(javaVersion = "17")
B. @EnabledOnJre(version = 17)
C. @EnabledOnJre(JRE.JAVA_17)
D. @EnabledOnJre(value = "JAVA_17")

Solution

  1. Step 1: Recall correct annotation usage

    The correct syntax uses the enum constant from JRE, like @EnabledOnJre(JRE.JAVA_17).
  2. Step 2: Compare options

    Only @EnabledOnJre(JRE.JAVA_17) uses the correct enum constant syntax. Others use invalid or incorrect attribute names or formats.
  3. Final Answer:

    @EnabledOnJre(JRE.JAVA_17) -> Option C
  4. Quick Check:

    Use enum JRE.JAVA_17 inside @EnabledOnJre [OK]
Hint: Use enum JRE.JAVA_17 inside parentheses [OK]
Common Mistakes:
  • Using string values instead of enum constants
  • Using incorrect attribute names like version or javaVersion
  • Missing the enum prefix JRE.
3. Given the test annotated with @EnabledOnJre({JRE.JAVA_11, JRE.JAVA_17}), what happens when running on Java 15?
medium
A. The test runs normally
B. The test is skipped
C. The test fails with an error
D. The test runs but logs a warning

Solution

  1. Step 1: Understand the annotation behavior

    The test runs only on Java 11 or Java 17 as specified.
  2. Step 2: Check Java 15 behavior

    Since Java 15 is not in the list, the test is skipped automatically.
  3. Final Answer:

    The test is skipped -> Option B
  4. Quick Check:

    Test runs only on specified JREs, else skipped [OK]
Hint: Test runs only on listed JREs; others skip [OK]
Common Mistakes:
  • Assuming test runs on any Java version
  • Expecting test to fail instead of skip
  • Thinking test logs a warning when skipped
4. Identify the error in this test annotation: @EnabledOnJre(JRE.JAVA_8, JRE.JAVA_11)
medium
A. JRE.JAVA_8 is not a valid enum constant
B. The annotation cannot be used with more than one JRE
C. The annotation requires a string, not enum constants
D. Multiple JREs must be inside curly braces {}

Solution

  1. Step 1: Check annotation syntax for multiple JREs

    When specifying multiple JREs, they must be enclosed in curly braces as an array.
  2. Step 2: Analyze given annotation

    The annotation uses parentheses with multiple enum constants without braces, which is invalid syntax.
  3. Final Answer:

    Multiple JREs must be inside curly braces {} -> Option D
  4. Quick Check:

    Multiple JREs need braces: {@code @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11})} [OK]
Hint: Use curly braces for multiple JREs inside @EnabledOnJre [OK]
Common Mistakes:
  • Omitting curly braces for multiple JREs
  • Using strings instead of enum constants
  • Thinking multiple JREs are not allowed
5. You want to write a test that runs only on Java 8, 11, and 17, but skips on all other versions. Which is the correct way to annotate the test?
hard
A. @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17})
B. @EnabledOnJre(JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17)
C. @EnabledOnJre(value = {"JAVA_8", "JAVA_11", "JAVA_17"})
D. @EnabledOnJre(versions = {JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17})

Solution

  1. Step 1: Recall correct syntax for multiple JREs

    Multiple JREs must be passed as an array inside curly braces using enum constants.
  2. Step 2: Evaluate options

    @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17}) correctly uses @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.
  3. Final Answer:

    @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17}) -> Option A
  4. Quick Check:

    Use enum array with braces for multiple JREs [OK]
Hint: Use enum array with braces for multiple JREs [OK]
Common Mistakes:
  • Missing curly braces for multiple JREs
  • Using strings instead of enum constants
  • Using incorrect attribute names like versions or value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard