0
0
JUnittesting~20 mins

Testing multiple exceptions in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Exceptions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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());
            }
        }
    }
}
ATest passes because IllegalArgumentException is one of the expected exceptions.
BTest fails with AssertionError because the exception message does not match.
CTest fails with AssertionError because NullPointerException was expected but IllegalArgumentException was thrown.
DTest fails with a compilation error due to incorrect assertThrowsAnyOf usage.
Attempts:
2 left
💡 Hint
Think about whether the thrown exception matches any of the expected exception classes.
assertion
intermediate
2: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
}
AassertThrows(FileNotFoundException.class, () -> methodUnderTest());
BassertThrows(IOException.class, () -> methodUnderTest());
CassertThrowsAnyOf(IOException.class, FileNotFoundException.class, () -> methodUnderTest());
DassertThrowsAllOf(IOException.class, FileNotFoundException.class, () -> methodUnderTest());
Attempts:
2 left
💡 Hint
Consider the exception class hierarchy; FileNotFoundException is a subclass of IOException.
🔧 Debug
advanced
2: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;
        });
    }
}
AThe test fails because the division by zero throws an unchecked exception.
BThe test fails because ArithmeticException is not caught by assertThrowsAnyOf.
CThe test fails because assertThrowsAnyOf is not a valid JUnit 5 method.
DThe test fails because the lambda does not throw any exception.
Attempts:
2 left
💡 Hint
Check if assertThrowsAnyOf is part of standard JUnit 5 API.
🧠 Conceptual
advanced
2: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?
AUse a custom helper method that catches exceptions and verifies if the thrown exception is one of the expected types.
BUse assertThrows with SQLException.class and then assertThrows with TimeoutException.class separately in the same test.
CUse assertThrowsAnyOf method from JUnit 5 standard library.
DUse try-catch blocks inside the test method and fail the test if the exception is not one of the expected types.
Attempts:
2 left
💡 Hint
JUnit 5 does not provide a built-in method for multiple expected exceptions.
framework
expert
3: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?
AUse a try-catch block inside each test method to manually check for multiple exceptions.
BOverride the default assertThrows method in JUnit 5 to accept multiple exception classes.
CUse JUnit 4's @Test(expected=...) annotation with multiple exception classes.
DCreate a custom annotation with expected exception classes and implement a TestExecutionExceptionHandler to verify thrown exceptions against them.
Attempts:
2 left
💡 Hint
JUnit 5 extensions can intercept test execution and handle exceptions.