What is the purpose of the @EnabledOnJre annotation in JUnit?
The @EnabledOnJre annotation is used to run a test only on specific Java Runtime Environment (JRE) versions. It helps to conditionally enable tests based on the JRE version.
Click to reveal answer
intermediate
How do you specify multiple JRE versions with @EnabledOnJre?
You specify multiple JRE versions by listing them inside the annotation like @EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11}). The test will run only if the current JRE matches any of these versions.
Click to reveal answer
beginner
Write a simple example of a test method enabled only on Java 17 using @EnabledOnJre.
```java
@EnabledOnJre(JRE.JAVA_17)
void testOnlyOnJava17() {
// test code here
}
```
Click to reveal answer
beginner
What happens if a test annotated with @EnabledOnJre runs on a non-specified JRE version?
The test is skipped and marked as disabled in the test report. It does not fail or pass; it simply does not run.
Click to reveal answer
intermediate
Why is it useful to use @EnabledOnJre in testing?
It helps avoid test failures caused by differences in JRE versions. Some features or behaviors may only exist in certain JREs, so this annotation ensures tests run only where they make sense.
Click to reveal answer
What does @EnabledOnJre(JRE.JAVA_11) do?
ARuns the test on all JRE versions except Java 11
BRuns the test only on Java 11 runtime
CDisables the test on Java 11 runtime
DRuns the test only on Java 8 runtime
✗ Incorrect
The annotation enables the test only if the current runtime is Java 11.
If you want a test to run on Java 8 and Java 17, how do you write @EnabledOnJre?
A@EnabledOnJre(JRE.JAVA_8 || JRE.JAVA_17)
B@EnabledOnJre(JRE.JAVA_8, JRE.JAVA_17)
C@EnabledOnJre({JRE.JAVA_8, JRE.JAVA_17})
D@EnabledOnJre(JRE.JAVA_8 && JRE.JAVA_17)
✗ Incorrect
You must use curly braces to specify multiple JRE versions as an array.
What is the test result if @EnabledOnJre specifies Java 17 but the test runs on Java 11?
ATest is skipped
BTest passes
CTest fails
DTest runs with warning
✗ Incorrect
The test is skipped because the current JRE does not match the specified version.
Which import is required to use @EnabledOnJre in JUnit 5?
The correct import is from the condition package: org.junit.jupiter.api.condition.EnabledOnJre.
Can @EnabledOnJre be combined with other conditional annotations like @EnabledOnOs?
ANo, it causes a compilation error
BNo, only one conditional annotation is allowed
CYes, but only if they specify the same condition
DYes, they can be combined to narrow test execution conditions
✗ Incorrect
Multiple conditional annotations can be combined to run tests only when all conditions are met.
Explain how @EnabledOnJre helps manage tests across different Java versions.
Think about why some tests might fail on older or newer Java versions.
You got /4 concepts.
Describe how to write a test that runs only on Java 8 and Java 11 using @EnabledOnJre.
Remember the syntax for multiple values in annotations.
You got /4 concepts.
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
Step 1: Understand the annotation purpose
<code>@EnabledOnJre
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.
Final Answer:
To run a test only on specified Java Runtime Environment versions -> Option A
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
Step 1: Recall correct annotation usage
The correct syntax uses the enum constant from JRE, like @EnabledOnJre(JRE.JAVA_17).
Step 2: Compare options
Only @EnabledOnJre(JRE.JAVA_17) uses the correct enum constant syntax. Others use invalid or incorrect attribute names or formats.
Final Answer:
@EnabledOnJre(JRE.JAVA_17) -> Option C
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
Step 1: Understand the annotation behavior
The test runs only on Java 11 or Java 17 as specified.
Step 2: Check Java 15 behavior
Since Java 15 is not in the list, the test is skipped automatically.
Final Answer:
The test is skipped -> Option B
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
Step 1: Check annotation syntax for multiple JREs
When specifying multiple JREs, they must be enclosed in curly braces as an array.
Step 2: Analyze given annotation
The annotation uses parentheses with multiple enum constants without braces, which is invalid syntax.
Final Answer:
Multiple JREs must be inside curly braces {} -> Option D
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
Step 1: Recall correct syntax for multiple JREs
Multiple JREs must be passed as an array inside curly braces using enum constants.
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.
Final Answer:
@EnabledOnJre({JRE.JAVA_8, JRE.JAVA_11, JRE.JAVA_17}) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.