Challenge - 5 Problems
OS-Specific Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the test execution result of this JUnit 5 code snippet?
Consider the following JUnit 5 test method annotated with @EnabledOnOs for Windows OS. What will be the test execution result if run on a Linux machine?
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class OSTest { @Test @EnabledOnOs(OS.WINDOWS) void testOnlyOnWindows() { System.out.println("Running Windows-specific test"); } }
Attempts:
2 left
💡 Hint
Think about what @EnabledOnOs does when the OS does not match.
✗ Incorrect
The annotation @EnabledOnOs(OS.WINDOWS) causes the test to run only on Windows OS. On Linux, the test is skipped and not executed.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies the OS name in a JUnit 5 test?
You want to write a test that asserts the current OS is Linux before running OS-specific logic. Which assertion is correct?
JUnit
String osName = System.getProperty("os.name").toLowerCase();Attempts:
2 left
💡 Hint
OS name strings can vary in case and format.
✗ Incorrect
System property "os.name" returns a string like "Linux" or "linux". Using toLowerCase() and contains("linux") is a safe way to check.
🔧 Debug
advanced2:30remaining
Why does this @EnabledOnOs test run on all OS instead of only Mac?
Examine the code below. The test is intended to run only on Mac OS but runs on Windows and Linux too. Identify the cause.
JUnit
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class MacTest { @Test @EnabledOnOs(os = OS.MAC) void testMacOnly() { System.out.println("Running Mac test"); } }
Attempts:
2 left
💡 Hint
Check the correct attribute name for @EnabledOnOs annotation.
✗ Incorrect
The correct attribute name for specifying OS in @EnabledOnOs is 'value', not 'os'. Using 'os' causes the annotation to ignore the condition and run the test on all OS.
❓ framework
advanced2:00remaining
How to run a test only on Windows and Linux using @EnabledOnOs?
You want a JUnit 5 test to run only on Windows and Linux OS. Which annotation usage is correct?
Attempts:
2 left
💡 Hint
Check how to specify multiple OS values in the annotation array.
✗ Incorrect
The @EnabledOnOs annotation accepts an array of OS enums. The correct syntax is @EnabledOnOs({OS.WINDOWS, OS.LINUX}).
🧠 Conceptual
expert3:00remaining
What happens if @EnabledOnOs is used on a test class with multiple test methods?
If a JUnit 5 test class is annotated with @EnabledOnOs(OS.MAC) and contains multiple test methods, what is the behavior when running on Windows?
Attempts:
2 left
💡 Hint
Class-level annotations affect all contained tests.
✗ Incorrect
When @EnabledOnOs is applied at the class level, it controls execution of all test methods inside. If the OS does not match, all tests are skipped.