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()
✗ Incorrect
assertThrows lets you test different exceptions separately in the same test method.
What does assertThrows return that can be useful?
AThe thrown exception object
BA boolean true/false
CThe method return value
DNothing
✗ 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?
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
✗ Incorrect
Each exception should be tested separately with assertThrows to confirm correct behavior.
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
✗ Incorrect
If the exception is not thrown, assertThrows causes the test to fail.
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
✗ 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.
Practice
(1/5)
1.
Which JUnit method is best to test that a specific exception is thrown by a method?
easy
A. assertThrows()
B. assertEquals()
C. assertTrue()
D. assertNull()
Solution
Step 1: Understand the purpose of assertThrows()
This method checks if a specific exception is thrown when running a code block.
Step 2: Compare with other assertion methods
assertEquals() checks values, assertTrue() checks boolean conditions, and assertNull() checks for null values, none test exceptions.
Final Answer:
assertThrows() -> Option A
Quick Check:
Test exceptions with assertThrows() [OK]
Hint: Use assertThrows() to check exceptions directly [OK]
Common Mistakes:
Using assertEquals() to test exceptions
Trying to catch exceptions manually in tests
Confusing assertTrue() with exception testing
2.
Which of the following is the correct syntax to test that IllegalArgumentException is thrown by myMethod() in JUnit 5?
?
easy
A. assertThrows(myMethod(), IllegalArgumentException.class);
B. assertThrows(() -> myMethod(), IllegalArgumentException.class);
C. assertThrows(IllegalArgumentException.class, () -> myMethod());
D. assertThrows(IllegalArgumentException, myMethod());
Solution
Step 1: Recall assertThrows() method signature
The correct order is the exception class first, then a lambda with the code to test.
Step 2: Check each option's syntax
assertThrows(IllegalArgumentException.class, () -> myMethod()); matches the correct syntax: assertThrows(Exception.class, () -> method());. Others have wrong order or missing lambda.
Final Answer:
assertThrows(IllegalArgumentException.class, () -> myMethod()); -> Option C
Quick Check:
Exception class first, then lambda [OK]
Hint: Exception class first, then lambda expression [OK]
Common Mistakes:
Swapping parameters order in assertThrows
Not using a lambda expression
Passing method call instead of lambda
3.
Given the following JUnit 5 test code, what will be the test result?
void testMultipleExceptions() {
assertThrows(NullPointerException.class, () -> { throw new NullPointerException(); });
assertThrows(IllegalStateException.class, () -> { throw new IllegalArgumentException(); });
}
medium
A. Test fails because second assertThrows expects IllegalStateException but gets IllegalArgumentException
B. Test passes because both exceptions are thrown
C. Test fails because NullPointerException is not thrown
D. Test passes because assertThrows ignores exception type
Solution
Step 1: Analyze first assertThrows
It expects NullPointerException and throws NullPointerException, so it passes.
Step 2: Analyze second assertThrows
It expects IllegalStateException but throws IllegalArgumentException, so it fails.
Final Answer:
Test fails because second assertThrows expects IllegalStateException but gets IllegalArgumentException -> Option A
Quick Check:
Expected exception must match thrown exception [OK]
Hint: Exception types must match exactly in assertThrows [OK]
Common Mistakes:
Assuming any exception passes assertThrows
Ignoring mismatch in exception types
Thinking assertThrows catches all exceptions
4.
Identify the error in the following JUnit 5 test code that tries to test two exceptions:
Assuming method2() throws IllegalStateException instead of IllegalArgumentException.
medium
A. The test will pass because all exceptions are thrown
B. The second assertThrows will fail because expected and actual exceptions differ
C. The test will fail because assertThrows cannot be called multiple times
D. The test will fail because method3() is not called
Solution
Step 1: Check expected vs actual exceptions for method2()
Expected is IllegalArgumentException but actual thrown is IllegalStateException, so assertThrows fails.
Step 2: Confirm other assertThrows calls
Calls for method1() and method3() are correct or irrelevant to this error.
Final Answer:
The second assertThrows will fail because expected and actual exceptions differ -> Option B
Quick Check:
Expected exception must match thrown exception [OK]
Hint: Check expected vs actual exception types carefully [OK]
Common Mistakes:
Assuming multiple assertThrows calls cause failure
Ignoring mismatch in exception types
Thinking assertThrows tests method calls, not exceptions
5.
You want to test that a method process() throws either IOException or TimeoutException depending on conditions. Which is the best way to write JUnit 5 tests for both exceptions?
hard
A. Use one assertThrows with both exceptions listed together
B. Use assertThrows only for the first exception and ignore the second
C. Catch exceptions manually inside one test and assert their types
D. Write two separate tests each using assertThrows for one exception
Solution
Step 1: Understand assertThrows usage
assertThrows tests one exception type per call; it cannot test multiple exceptions at once.
Step 2: Best practice for multiple exceptions
Write separate tests for each expected exception to keep tests clear and focused.
Final Answer:
Write two separate tests each using assertThrows for one exception -> Option D
Quick Check:
One assertThrows per exception per test [OK]
Hint: Test each exception in its own assertThrows call [OK]
Common Mistakes:
Trying to test multiple exceptions in one assertThrows
Catching exceptions manually instead of using assertThrows
Ignoring one exception in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.