We check exception type hierarchy to make sure the right kind of error happens. This helps us catch mistakes early and fix them.
Checking exception type hierarchy in JUnit
assertThrows(ExpectedException.class, () -> {
// code that should throw exception
});assertThrows checks if the code throws the expected exception or any subclass of it.
The first argument is the exception class you expect.
FileNotFoundException is a subclass of IOException.assertThrows(IOException.class, () -> {
throw new FileNotFoundException();
});NullPointerException is a subclass of RuntimeException.assertThrows(RuntimeException.class, () -> {
throw new NullPointerException();
});IllegalArgumentException is a subclass of Exception.assertThrows(Exception.class, () -> {
throw new IllegalArgumentException();
});IllegalArgumentException is NOT a subclass of IOException.assertThrows(IOException.class, () -> {
throw new IllegalArgumentException();
});The first test passes because FileNotFoundException is a subclass of IOException. The second test fails because IllegalArgumentException is not related to IOException.
import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; import java.io.FileNotFoundException; import java.io.IOException; public class ExceptionHierarchyTest { @Test void testFileNotFoundExceptionIsIOException() { assertThrows(IOException.class, () -> { throw new FileNotFoundException("File missing"); }); } @Test void testIllegalArgumentExceptionIsNotIOException() { assertThrows(IOException.class, () -> { throw new IllegalArgumentException("Bad argument"); }); } }
Use assertThrows to check exception types and their subclasses easily.
If the thrown exception is not the expected type or subclass, the test fails with a clear message.
This helps catch errors early and documents expected error behavior in your tests.
assertThrows checks if code throws an expected exception or its subclass.
Tests pass if the thrown exception matches or inherits from the expected type.
Tests fail if a different exception type is thrown.
Practice
Solution
Step 1: Understand the purpose of assertThrows()
assertThrows() is designed to check if a block of code throws an exception of a specified type or its subclass.Step 2: Compare with other assertions
assertEquals() checks equality, assertTrue() checks boolean conditions, and assertNull() checks for null values, none of which check exceptions.Final Answer:
assertThrows() -> Option AQuick Check:
Exception type check = assertThrows() [OK]
- Using assertEquals() to check exceptions
- Confusing assertTrue() with exception checks
- Expecting assertNull() to verify exceptions
Solution
Step 1: Recall assertThrows() method signature
assertThrows() takes the exception class as the first argument and a lambda expression as the second.Step 2: Match the syntax
assertThrows(NullPointerException.class, () -> method()); correctly uses assertThrows(NullPointerException.class, () -> method()); which matches the required syntax.Final Answer:
assertThrows(NullPointerException.class, () -> method()); -> Option AQuick Check:
Correct syntax = assertThrows(NullPointerException.class, () -> method()); [OK]
- Passing exception instance instead of class
- Swapping argument order in assertThrows()
- Not using lambda expression for the executable
assertThrows(RuntimeException.class, () -> { throw new IllegalArgumentException(); });What will be the result of this test?
Solution
Step 1: Check exception hierarchy
IllegalArgumentException extends RuntimeException, so it is a subclass.Step 2: Understand assertThrows behavior
assertThrows(RuntimeException.class, ...) passes if the thrown exception is RuntimeException or any subclass.Final Answer:
Test passes because IllegalArgumentException is a subclass of RuntimeException -> Option CQuick Check:
Subclass exception passes assertThrows superclass check [OK]
- Thinking exact exception type is required
- Confusing subclass with unrelated exceptions
- Expecting compilation error for subclass exceptions
assertThrows(RuntimeException.class, () -> { throw new FileNotFoundException(); });The test fails. What is the most likely reason?
Solution
Step 1: Check exception hierarchy
FileNotFoundException extends IOException, not RuntimeException.Step 2: Understand assertThrows behavior
assertThrows(RuntimeException.class, ...) fails because the thrown exception is not RuntimeException or any subclass. Other options are incorrect: checked exceptions like FileNotFoundException do not require declaration in the test method when using Executable lambdas.Final Answer:
FileNotFoundException is not a subclass of RuntimeException -> Option DQuick Check:
Non-subclass exception causes assertThrows to fail [OK]
- Misidentifying exception subclass relationships
- Ignoring checked exception declaration rules
- Expecting runtime exceptions to behave like checked
Solution
Step 1: Understand assertThrows behavior with superclass
assertThrows(IOException.class, ...) passes if the thrown exception is IOException or any subclass.Step 2: Evaluate other options
Use assertThrows(Exception.class, () -> method()); and check instance with instanceof IOException requires manual checks and complicates test; Use try-catch inside test and assert exception type manually is verbose; Use assertThrows(Throwable.class, () -> method()); is too broad and accepts all throwables.Final Answer:
Use assertThrows(IOException.class, () -> method()); -> Option BQuick Check:
Superclass in assertThrows covers subclasses [OK]
- Using too broad exception classes
- Manually checking exception types unnecessarily
- Ignoring assertThrows built-in subclass support
Ready to prove it?
Pass the test and this pattern moves unknown
