Challenge - 5 Problems
Exception Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JUnit test when the method throws the expected exception?
Consider this JUnit 5 test method that expects an IllegalArgumentException. What will be the test result when the tested method throws this exception?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrows; class ExampleTest { @Test void testException() { assertThrows(IllegalArgumentException.class, () -> { throw new IllegalArgumentException("Invalid argument"); }); } }
Attempts:
2 left
💡 Hint
Remember that assertThrows expects the specified exception type to be thrown for the test to pass.
✗ Incorrect
The assertThrows method in JUnit passes the test if the specified exception type is thrown during the execution of the lambda. Since IllegalArgumentException is thrown, the test passes.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies that a method throws NullPointerException?
You want to write a JUnit 5 test that confirms a method throws NullPointerException when called with null. Which assertion is correct?
Attempts:
2 left
💡 Hint
Use the assertion designed to check for exceptions.
✗ Incorrect
assertThrows checks that the specified exception is thrown by the code inside the lambda. The other options either check wrong things or expect no exception.
🧠 Conceptual
advanced2:00remaining
Why is exception testing important for validating error handling?
Which statement best explains why exception testing is crucial in software testing?
Attempts:
2 left
💡 Hint
Think about how programs should behave when something goes wrong.
✗ Incorrect
Exception testing confirms that the program detects error conditions and responds by throwing the correct exceptions, allowing graceful error handling.
🔧 Debug
advanced2:00remaining
Identify the problem in this exception test code
This JUnit 5 test is supposed to check that a method throws IndexOutOfBoundsException, but it always passes even if no exception is thrown. What is wrong?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrows; class DebugTest { @Test void testException() { try { methodThatShouldThrow(); } catch (IndexOutOfBoundsException e) { // expected } } void methodThatShouldThrow() { // no exception thrown here } }
Attempts:
2 left
💡 Hint
Consider how JUnit knows a test should fail when an exception is missing.
✗ Incorrect
Without assertThrows or a fail() call after the try block, the test passes even if no exception occurs, because the catch block is never entered and no failure is triggered.
❓ framework
expert2:00remaining
How does JUnit 5's assertThrows improve exception testing compared to older methods?
Which feature of assertThrows in JUnit 5 makes exception testing more reliable and readable than older try-catch based tests?
Attempts:
2 left
💡 Hint
Think about how assertThrows simplifies test code and failure detection.
✗ Incorrect
assertThrows automatically fails the test if the expected exception is not thrown, making tests cleaner and less error-prone compared to manual try-catch blocks.