Recall & Review
beginner
What does it mean to check exception type hierarchy in testing?
It means verifying that the thrown exception is of the expected type or a subtype, ensuring the test catches the correct error class.
Click to reveal answer
beginner
How can you check for a specific exception type in JUnit 5?
Use the assertThrows method with the expected exception class and a lambda that runs the code that should throw the exception.Click to reveal answer
intermediate
Why is it important to check the exception type hierarchy rather than just the base exception?
Because catching only the base exception might hide specific errors; checking the exact type or subtype ensures precise error handling and test accuracy.
Click to reveal answer
intermediate
Example: What exception type will assertThrows catch if the thrown exception is a subclass of the expected exception?assertThrows will pass if the thrown exception is the expected type or any subclass of it, because it checks the exception type hierarchy.Click to reveal answer
beginner
How to write a JUnit 5 test that verifies a method throws IOException or any subclass of IOException?Use assertThrows(IOException.class, () -> methodThatThrows()); This passes if the thrown exception is IOException or any subclass like FileNotFoundException.Click to reveal answer
In JUnit 5, which method is used to check if a specific exception or its subclass is thrown?
✗ Incorrect
assertThrows checks if the code throws the expected exception type or any subclass of it.
If a method throws FileNotFoundException, which is a subclass of IOException, what happens when you assertThrows(IOException.class, ...)?
✗ Incorrect
assertThrows accepts the expected exception type or any subclass, so the test passes.
Why should you avoid catching only the base Exception class in tests?
✗ Incorrect
Catching only base Exception hides specific exception types, reducing test accuracy.
Which JUnit 5 feature helps you check the exception message along with the type?
✗ Incorrect
assertThrows returns the caught exception so you can assert its message or other properties.
What happens if the tested code does not throw any exception but assertThrows expects one?
✗ Incorrect
assertThrows fails the test if no exception is thrown.
Explain how to use JUnit 5 to check that a method throws a specific exception or any of its subclasses.
Think about how assertThrows works with exception types.
You got /4 concepts.
Why is it important to consider exception type hierarchy when writing tests for exceptions?
Consider how Java exception classes relate to each other.
You got /4 concepts.