Recall & Review
beginner
What is the purpose of testing multiple exceptions in JUnit?
To verify that a method throws the correct exceptions under different error conditions, ensuring robust error handling.
Click to reveal answer
intermediate
How can you test multiple exceptions in JUnit 5 using assertThrows?
Use separate assertThrows calls for each exception scenario, each with a lambda that triggers the exception.
Click to reveal answer
intermediate
What is the advantage of using assertThrows over the @Test(expected=...) annotation?
assertThrows allows testing multiple exceptions in the same test method and lets you inspect the exception object for details.
Click to reveal answer
intermediate
Example: How do you test that a method throws IllegalArgumentException and NullPointerException separately in one test?
Use two assertThrows calls: one for IllegalArgumentException and one for NullPointerException, each with code triggering that exception.
Click to reveal answer
beginner
Why is it important to test multiple exceptions in one test method?
It saves time and keeps related exception tests together, making the test suite easier to maintain and understand.
Click to reveal answer
Which JUnit method is best for testing multiple exceptions in one test?
✗ Incorrect
assertThrows lets you test different exceptions separately in the same test method.
What does assertThrows return that can be useful?
✗ Incorrect
assertThrows returns the exception object so you can check its message or properties.
If a method can throw NullPointerException and IllegalArgumentException, how should you test it?
✗ Incorrect
Each exception should be tested separately with assertThrows to confirm correct behavior.
What happens if the expected exception is not thrown in assertThrows?
✗ Incorrect
If the exception is not thrown, assertThrows causes the test to fail.
Why might @Test(expected=Exception.class) be less flexible than assertThrows?
✗ Incorrect
@Test(expected=...) can only check one exception and cannot inspect the exception details.
Explain how to test multiple exceptions in a single JUnit 5 test method using assertThrows.
Think about calling assertThrows multiple times inside one test.
You got /4 concepts.
Why is testing multiple exceptions important in software testing?
Consider what happens if exceptions are not tested.
You got /4 concepts.