0
0
JUnittesting~5 mins

Testing multiple exceptions in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A@Test(expected=Exception.class)
BassertThrows
CassertEquals
Dfail()
What does assertThrows return that can be useful?
AThe thrown exception object
BA boolean true/false
CThe method return value
DNothing
If a method can throw NullPointerException and IllegalArgumentException, how should you test it?
AUse two assertThrows calls, one for each exception
BUse one assertThrows call for both exceptions
CUse @Test(expected=Exception.class)
DNo need to test exceptions
What happens if the expected exception is not thrown in assertThrows?
AThe test is skipped
BThe test passes
CAn error is logged but test passes
DThe test fails
Why might @Test(expected=Exception.class) be less flexible than assertThrows?
AIt can only test one exception per test method
BIt does not check exception messages
CBoth A and C
DIt requires more code
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.