Challenge - 5 Problems
Multiple Exceptions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of JUnit test with multiple expected exceptions
Consider the following JUnit 5 test method that expects either
IllegalArgumentException or NullPointerException. What will be the test result when IllegalArgumentException is thrown?JUnit
import org.junit.jupiter.api.Test; public class ExceptionTest { @Test void testMultipleExceptions() { assertThrowsAnyOf(IllegalArgumentException.class, NullPointerException.class, () -> { throw new IllegalArgumentException("Invalid argument"); }); } static void assertThrowsAnyOf(Class<? extends Throwable> ex1, Class<? extends Throwable> ex2, Runnable executable) { try { executable.run(); throw new AssertionError("Expected exception was not thrown"); } catch (Throwable t) { if (!ex1.isInstance(t) && !ex2.isInstance(t)) { throw new AssertionError("Unexpected exception type: " + t.getClass()); } } } }
Attempts:
2 left
💡 Hint
Think about whether the thrown exception matches any of the expected exception classes.
✗ Incorrect
The custom assertThrowsAnyOf method checks if the thrown exception is an instance of either expected exception class. Since IllegalArgumentException is expected, the test passes.
❓ assertion
intermediate2:00remaining
Correct assertion for multiple exceptions in JUnit 5
Which of the following JUnit 5 assertions correctly verifies that a method throws either
IOException or FileNotFoundException?JUnit
void methodUnderTest() throws IOException {
// implementation
}Attempts:
2 left
💡 Hint
Consider the exception class hierarchy; FileNotFoundException is a subclass of IOException.
✗ Incorrect
assertThrows(IOException.class, ...) succeeds because FileNotFoundException is a subclass of IOException, so either will match. JUnit 5 lacks built-in assertThrowsAnyOf.
🔧 Debug
advanced2:00remaining
Debug failing JUnit test with multiple exceptions
This JUnit 5 test is intended to pass if either
ArithmeticException or NullPointerException is thrown. However, it always fails. Identify the cause.JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrowsAnyOf; public class DebugTest { @Test void testException() { assertThrowsAnyOf(ArithmeticException.class, NullPointerException.class, () -> { int x = 1 / 0; }); } }
Attempts:
2 left
💡 Hint
Check if assertThrowsAnyOf is part of standard JUnit 5 API.
✗ Incorrect
JUnit 5 does not have an assertThrowsAnyOf method. This causes a compilation error, so the test cannot run and always fails.
🧠 Conceptual
advanced2:00remaining
Best practice for testing multiple exceptions in JUnit 5
Which approach is best to test that a method throws either
SQLException or TimeoutException in JUnit 5?Attempts:
2 left
💡 Hint
JUnit 5 does not provide a built-in method for multiple expected exceptions.
✗ Incorrect
JUnit 5 does not have assertThrowsAnyOf. The best practice is to write a custom helper that asserts the thrown exception is one of the expected types, making the test clear and maintainable.
❓ framework
expert3:00remaining
JUnit 5 extension for multiple exception assertions
You want to create a reusable JUnit 5 extension that allows asserting multiple possible exceptions from a test method. Which of the following correctly describes how to implement this?
Attempts:
2 left
💡 Hint
JUnit 5 extensions can intercept test execution and handle exceptions.
✗ Incorrect
JUnit 5 allows creating extensions that can intercept exceptions thrown by tests. A custom annotation combined with TestExecutionExceptionHandler can check if the thrown exception matches any expected type, enabling reusable multiple exception assertions.