What is the purpose of the @EnabledOnOs annotation in JUnit?
It allows a test to run only on specified operating systems, skipping it on others. This helps test OS-specific features or behaviors.
Click to reveal answer
beginner
How do you specify multiple operating systems with @EnabledOnOs?
You list them inside the annotation using the value attribute, for example: @EnabledOnOs({OS.WINDOWS, OS.LINUX}).
Click to reveal answer
beginner
What happens if a test annotated with @EnabledOnOs(OS.MAC) runs on Windows?
The test is skipped and not executed because the current OS does not match the specified one.
Click to reveal answer
beginner
Write a simple JUnit 5 test method that runs only on Linux using @EnabledOnOs.
```java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
class MyTests {
@Test
@EnabledOnOs(OS.LINUX)
void testOnlyOnLinux() {
// test code here
}
}
```
Click to reveal answer
beginner
Why is it useful to run tests only on specific operating systems?
Because some features or bugs appear only on certain OSes. Running OS-specific tests prevents false failures on unsupported systems and saves test time.
Click to reveal answer
Which annotation runs a JUnit test only on Windows OS?
A@EnabledOnOs(OS.WINDOWS)
B@DisabledOnOs(OS.WINDOWS)
C@EnabledIfOs(OS.WINDOWS)
D@RunOnOs(OS.WINDOWS)
✗ Incorrect
The correct annotation to enable a test only on Windows is @EnabledOnOs(OS.WINDOWS).
What happens if a test annotated with @EnabledOnOs(OS.LINUX) runs on Mac?
ATest runs normally
BTest runs but logs a warning
CTest fails
DTest is skipped
✗ Incorrect
The test is skipped because the OS does not match the specified Linux OS.
How do you specify multiple OSes in @EnabledOnOs?
A@EnabledOnOs(OS.WINDOWS, OS.MAC)
B@EnabledOnOs({OS.WINDOWS, OS.MAC})
C@EnabledOnOs[OS.WINDOWS, OS.MAC]
D@EnabledOnOs<OS.WINDOWS, OS.MAC>
✗ Incorrect
Multiple OSes are specified as an array: @EnabledOnOs({OS.WINDOWS, OS.MAC}).
Which package contains the @EnabledOnOs annotation?
Aorg.junit.jupiter.api.enabled
Borg.junit.jupiter.api.os
Corg.junit.jupiter.api.condition
Dorg.junit.platform.condition
✗ Incorrect
@EnabledOnOs is in the package org.junit.jupiter.api.condition.
Why might you want to skip tests on certain OSes?
AAll of the above
BTo avoid false failures due to OS differences
CBecause some features only exist on certain OSes
DTo reduce test runtime on unsupported OSes
✗ Incorrect
All these reasons justify skipping tests on unsupported OSes.
Explain how to use @EnabledOnOs to run a test only on Windows and Mac OS.
Think about how to list multiple OS values inside the annotation.
You got /4 concepts.
Describe the benefits of using OS-specific test annotations like @EnabledOnOs in your test suite.
Consider how different OSes can affect software behavior and testing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @EnabledOnOs annotation in JUnit?
easy
A. To specify the Java version for a test
B. To disable a test permanently
C. To run tests in parallel
D. To run a test only on specified operating systems
Solution
Step 1: Understand the role of @EnabledOnOs
This annotation controls on which operating systems a test should run.
Step 2: Compare with other options
Options B, C, and D describe different functionalities unrelated to OS-specific test execution.
Final Answer:
To run a test only on specified operating systems -> Option D
Quick Check:
@EnabledOnOs = Run test on specific OS [OK]
Hint: Think: runs tests only on chosen OS platforms [OK]
Common Mistakes:
Confusing @EnabledOnOs with disabling tests
Assuming it controls Java version
Thinking it manages test parallelism
2. Which of the following is the correct syntax to enable a test only on Windows OS using @EnabledOnOs?
easy
A. @EnabledOnOs(OS.WINDOWS)
B. @EnabledOnOs(OS.WIN)
C. @EnabledOnOs("Windows")
D. @EnabledOnOs(os = "Windows")
Solution
Step 1: Recall the correct enum usage
The @EnabledOnOs annotation uses the enum OS with constants like WINDOWS.
Step 2: Check each option's syntax
@EnabledOnOs(OS.WINDOWS) uses the correct enum constant OS.WINDOWS. Options A, B, and C use invalid syntax or strings instead of enum constants.
Final Answer:
@EnabledOnOs(OS.WINDOWS) -> Option A
Quick Check:
Use enum OS.WINDOWS for Windows [OK]
Hint: Use enum OS constants, not strings or wrong names [OK]
Common Mistakes:
Using string literals instead of enum constants
Using incorrect enum names like OS.WIN
Adding parameters incorrectly
3. Given the following test code snippet, what will happen when running on Linux OS?
@Test
@EnabledOnOs(OS.WINDOWS)
void testOnlyOnWindows() {
System.out.println("Running Windows test");
}
medium
A. The test will run and print "Running Windows test"
B. The test will be skipped and not run
C. The test will fail with an exception
D. The test will run but print nothing
Solution
Step 1: Understand the annotation effect on Linux
The test is enabled only on Windows OS, so on Linux it should not run.
Step 2: Determine test behavior on unsupported OS
JUnit skips tests annotated with @EnabledOnOs if the current OS does not match.
Final Answer:
The test will be skipped and not run -> Option B
Quick Check:
Test runs only on Windows, skipped on Linux [OK]
Hint: If OS doesn't match, test is skipped automatically [OK]
Common Mistakes:
Assuming test runs but prints nothing
Expecting a failure or exception
Thinking test runs on all OS by default
4. Identify the error in this test code snippet:
@Test
@EnabledOnOs(OS.LINUX, OS.MAC)
void testOnUnix() {
// test code
}
medium
A. OS.LINUX and OS.MAC are invalid enum constants
B. The annotation should be @DisabledOnOs instead
C. Multiple OS values must be inside an array
D. The method must be static to use @EnabledOnOs
Solution
Step 1: Check annotation parameter format
The @EnabledOnOs annotation expects an array of OS enums when specifying multiple OSes.
Step 2: Identify correct syntax
Correct syntax is @EnabledOnOs({OS.LINUX, OS.MAC}). The code misses curly braces for the array. Thus, Multiple OS values must be inside an array correctly identifies that multiple OS values must be inside an array.
Final Answer:
Multiple OS values must be inside an array -> Option C
Quick Check:
Use curly braces for multiple OS enums [OK]
Hint: Use curly braces {} for multiple OS enums [OK]
Common Mistakes:
Omitting curly braces for multiple OS values
Confusing @EnabledOnOs with @DisabledOnOs
Thinking method must be static
5. You want a test to run only on Windows and Mac OS but skip on Linux. Which is the correct way to write the @EnabledOnOs annotation for this requirement?
hard
A. @EnabledOnOs({OS.WINDOWS, OS.MAC})
B. @EnabledOnOs(OS.WINDOWS, OS.MAC, OS.LINUX)
C. @EnabledOnOs(OS.WINDOWS) @EnabledOnOs(OS.MAC)
D. @EnabledOnOs(OS.WINDOWS & OS.MAC)
Solution
Step 1: Understand how to specify multiple OSes
Multiple OSes must be provided as an array inside curly braces in @EnabledOnOs.
Step 2: Evaluate each option
@EnabledOnOs({OS.WINDOWS, OS.MAC}) correctly uses an array with OS.WINDOWS and OS.MAC. @EnabledOnOs(OS.WINDOWS, OS.MAC, OS.LINUX) includes Linux, which should be excluded. @EnabledOnOs(OS.WINDOWS) @EnabledOnOs(OS.MAC) uses multiple annotations, which is invalid. @EnabledOnOs(OS.WINDOWS & OS.MAC) uses an invalid bitwise operator.
Final Answer:
@EnabledOnOs({OS.WINDOWS, OS.MAC}) -> Option A
Quick Check:
Use array with OS enums to include multiple OSes [OK]
Hint: Use curly braces with OS enums for multiple OSes [OK]
Common Mistakes:
Including unwanted OS like Linux
Using multiple @EnabledOnOs annotations
Using invalid operators like &
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.