0
0
JUnittesting~5 mins

@EnabledOnOs for OS-specific tests in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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)
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
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>
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
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
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.