Challenge - 5 Problems
Exception Message Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Check exception message with JUnit 5
Which JUnit 5 assertion correctly verifies that a method throws an IllegalArgumentException with the message "Invalid input"?
JUnit
public void testInvalidInput() {
// methodUnderTest throws IllegalArgumentException with message "Invalid input" when input is negative
}Attempts:
2 left
💡 Hint
Use assertThrows to capture the exception, then check its message separately.
✗ Incorrect
Option B correctly uses assertThrows to capture the exception object, then asserts the message with assertEquals. Option B incorrectly passes the message as a third argument to assertThrows, which is for failure message, not exception message. Option B uses AssertJ syntax, not JUnit 5 core. Option B calls getMessage() but does not assert the result, so it does not fail the test if message is wrong.
❓ Predict Output
intermediate2:00remaining
Output of JUnit test with exception message assertion
What is the result of running this JUnit 5 test method if methodUnderTest throws IllegalArgumentException with message "Wrong value" instead of "Invalid input"?
JUnit
import static org.junit.jupiter.api.Assertions.*; @Test void testExceptionMessage() { IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> methodUnderTest(-5)); assertEquals("Invalid input", ex.getMessage()); } void methodUnderTest(int x) { if (x < 0) throw new IllegalArgumentException("Wrong value"); }
Attempts:
2 left
💡 Hint
Check the difference between expected and actual exception messages.
✗ Incorrect
The test captures the exception correctly but asserts the wrong message. The actual message is 'Wrong value' but the test expects 'Invalid input', so assertEquals fails with an AssertionError showing the mismatch.
🔧 Debug
advanced2:00remaining
Identify the error in exception message assertion
What is wrong with this JUnit 5 test code that tries to check the exception message?
JUnit
assertThrows(IllegalArgumentException.class, () -> methodUnderTest(-1), msg -> assertEquals("Invalid input", msg));
Attempts:
2 left
💡 Hint
Check the method signature of assertThrows in JUnit 5.
✗ Incorrect
The third argument to assertThrows is an optional failure message string, not a lambda function. Passing a lambda causes a compilation error because no matching method signature exists.
❓ framework
advanced2:00remaining
Best practice for checking exception messages in JUnit 5
Which approach is considered best practice for verifying exception messages in JUnit 5 tests?
Attempts:
2 left
💡 Hint
Consider readability and clarity of test code.
✗ Incorrect
Option A is best practice because it uses assertThrows to capture the exception object clearly and then asserts the message explicitly. Option A is verbose and less clear. Option A is invalid syntax. Option A is deprecated in JUnit 5 and does not allow message checking.
🧠 Conceptual
expert2:00remaining
Why check exception messages in tests?
Why is it important to verify the exception message in addition to the exception type in automated tests?
Attempts:
2 left
💡 Hint
Think about why the same exception type might be thrown for different reasons.
✗ Incorrect
Exception types can be generic and used in multiple places. Checking the message ensures the test verifies the exact error scenario, avoiding false positives when the same exception type is thrown for different reasons.