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?
AassertTrue
BassertEquals
CassertThrows
DassertNull
✗ 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, ...)?
ATest fails because exception types must match exactly
BTest fails because IOException is a superclass
CTest passes only if FileNotFoundException is caught explicitly
DTest passes because FileNotFoundException is a subclass of IOException
✗ 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?
AIt can hide specific exceptions and make tests less precise
BIt makes tests run slower
CIt causes syntax errors
DIt is required to catch only base Exception
✗ 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?
AassertTrue
BassertThrows returns the exception object for further checks
CassertEquals
DassertNull
✗ 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?
AThe test fails because no exception was thrown
BThe test passes
CThe test is skipped
DThe test throws a compilation error
✗ 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.
Practice
(1/5)
1. In JUnit, when you want to check if a method throws an exception of a specific type or any of its subclasses, which assertion method should you use?
easy
A. assertThrows()
B. assertEquals()
C. assertTrue()
D. assertNull()
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 A
Quick Check:
Exception type check = assertThrows() [OK]
Hint: Use assertThrows() to check exceptions and their subclasses [OK]
Common Mistakes:
Using assertEquals() to check exceptions
Confusing assertTrue() with exception checks
Expecting assertNull() to verify exceptions
2. Which of the following is the correct syntax to assert that a method throws a NullPointerException in JUnit 5?
easy
A. assertThrows(NullPointerException.class, () -> method());
B. assertThrows(NullPointerException, method());
C. assertThrows(() -> method(), NullPointerException.class);
D. assertThrows(method(), NullPointerException);
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 A
Hint: assertThrows accepts subclass exceptions of the specified class [OK]
Common Mistakes:
Thinking exact exception type is required
Confusing subclass with unrelated exceptions
Expecting compilation error for subclass exceptions
4. Given this JUnit 5 test:
assertThrows(RuntimeException.class, () -> { throw new FileNotFoundException(); });
The test fails. What is the most likely reason?
medium
A. The lambda expression does not throw any exception
B. The test method does not declare throws IOException
C. FileNotFoundException is a checked exception and must be declared
D. FileNotFoundException is not a subclass of RuntimeException
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 D
Quick Check:
Non-subclass exception causes assertThrows to fail [OK]
Hint: assertThrows requires the thrown exception to be the specified class or subclass [OK]
Common Mistakes:
Misidentifying exception subclass relationships
Ignoring checked exception declaration rules
Expecting runtime exceptions to behave like checked
5. You want to write a JUnit 5 test that passes if a method throws either an IOException or any of its subclasses, but fails for other exceptions. Which of the following is the best approach?
hard
A. Use assertThrows(Exception.class, () -> method()); and check instance with instanceof IOException
B. Use assertThrows(IOException.class, () -> method());
C. Use try-catch inside test and assert exception type manually
D. Use assertThrows(Throwable.class, () -> method());
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 B
Quick Check:
Superclass in assertThrows covers subclasses [OK]
Hint: Use superclass exception in assertThrows to cover subclasses [OK]
Common Mistakes:
Using too broad exception classes
Manually checking exception types unnecessarily
Ignoring assertThrows built-in subclass support
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.