Challenge - 5 Problems
Exception Hierarchy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
JUnit Exception Hierarchy: Which exception is caught?
Consider the following JUnit test method. Which exception type will be caught and asserted in this test?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class ExceptionTest { @Test void testExceptionHierarchy() { Exception thrown = assertThrows(Exception.class, () -> { throw new IllegalArgumentException("Invalid argument"); }); assertEquals(IllegalArgumentException.class, thrown.getClass()); } }
Attempts:
2 left
💡 Hint
Remember that IllegalArgumentException extends RuntimeException, which extends Exception.
✗ Incorrect
In JUnit, assertThrows catches exceptions of the specified class or any subclass. Since IllegalArgumentException extends Exception, it is caught and the test passes. The actual exception type is IllegalArgumentException, so the assertion on getClass() succeeds.
❓ assertion
intermediate1:30remaining
Correct Assertion for Exception Subclass in JUnit
You want to assert that a method throws a NullPointerException using JUnit 5. Which assertion correctly verifies the exception type?
Attempts:
2 left
💡 Hint
Use the most specific exception type to verify the exact exception thrown.
✗ Incorrect
assertThrows expects the exact exception class or subclass you want to verify. Using NullPointerException.class ensures the test only passes if that specific exception is thrown.
🔧 Debug
advanced2:30remaining
Why does this JUnit test fail to catch the exception?
Examine the following JUnit test. Why does it fail even though the method throws an IOException?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class FileTest { @Test void testIOException() { assertThrows(FileNotFoundException.class, () -> { throw new IOException("File error"); }); } }
Attempts:
2 left
💡 Hint
Check the inheritance relationship between IOException and FileNotFoundException.
✗ Incorrect
FileNotFoundException extends IOException, so it is a subclass. But IOException is a superclass and not caught when expecting FileNotFoundException specifically. The thrown exception must be the same or subclass of the asserted exception.
🧠 Conceptual
advanced1:30remaining
Understanding Exception Type Matching in JUnit assertThrows
In JUnit 5, when using assertThrows(ExpectedException.class, executable), which of the following statements is true about the exception matching?
Attempts:
2 left
💡 Hint
Think about inheritance and polymorphism in Java exceptions.
✗ Incorrect
assertThrows matches exceptions that are instances of the specified class or any subclass. This allows catching more specific exceptions when expecting a general exception type.
❓ framework
expert3:00remaining
JUnit 5 Exception Assertion with Multiple Exception Types
You want to write a JUnit 5 test that passes if either IllegalArgumentException or NullPointerException is thrown by a method. Which approach correctly achieves this?
Attempts:
2 left
💡 Hint
Check if JUnit 5 has a built-in method to assert multiple exception types.
✗ Incorrect
JUnit 5 does not have assertThrowsAnyOf, but using assertThrows(Throwable.class, ...) to capture the exception and then asserting it is an instance of either expected type (e.g., via instanceof checks or getClass()) is the standard and correct approach.