What if your tests could instantly catch the exact error message your users see?
Why Checking exception message in JUnit? - Purpose & Use Cases
Imagine you are testing a program manually and want to make sure it shows the right error message when something goes wrong, like dividing by zero.
You run the program, see the error popup, and try to remember the exact message to check later.
Manually checking error messages is slow and easy to forget or misread.
You might miss small differences in wording that matter, or spend too much time repeating the same checks.
Using JUnit to check exception messages automatically catches the exact error text during tests.
This saves time, avoids mistakes, and makes sure your program tells users the right thing when errors happen.
try { divide(10, 0); fail("Expected exception"); } catch (Exception e) { if (!e.getMessage().equals("Cannot divide by zero")) { fail("Wrong message"); } }
assertThrows(ArithmeticException.class, () -> divide(10, 0), "Cannot divide by zero");
You can trust your tests to catch exact error messages, making your software more reliable and user-friendly.
When a banking app prevents a withdrawal that exceeds the balance, checking the exception message ensures the user sees a clear, correct warning.
Manual error message checks are slow and error-prone.
JUnit lets you verify exact exception messages automatically.
This improves test accuracy and saves time.
Practice
Solution
Step 1: Understand how to catch exceptions in JUnit 5
JUnit 5 uses assertThrows to check if a block of code throws an exception.Step 2: Check the exception message
After assertThrows returns the exception, you can assert its message with assertEquals or other assertions.Final Answer:
assertThrows with a lambda and then assert on the exception message -> Option AQuick Check:
assertThrows + message check = A [OK]
- Trying to check exception message inside assertEquals directly
- Using assertTrue with exception message string only
- Expecting assertNull to check exceptions
Solution
Step 1: Use assertThrows with exception class and lambda
The correct order is assertThrows(ExceptionClass.class, executable lambda).Step 2: Get the exception message and compare with equals()
Use getMessage() and equals() to compare strings, not ==.Final Answer:
assertEquals("Invalid input", assertThrows(IllegalArgumentException.class, () -> method()).getMessage()); -> Option BQuick Check:
assertThrows + getMessage + equals = B [OK]
- Passing message as third argument to assertThrows
- Swapping lambda and exception class order
- Using == to compare strings instead of equals()
Exception exception = assertThrows(NullPointerException.class, () -> { throw new NullPointerException("Null value found"); });
assertEquals("Null value found", exception.getMessage());Solution
Step 1: Check exception type thrown matches assertThrows
The lambda throws NullPointerException, which matches the expected type.Step 2: Check the exception message matches the assertEquals
The message "Null value found" matches exactly, so assertion passes.Final Answer:
Test passes because exception type and message match -> Option CQuick Check:
Exception type and message match = Pass [OK]
- Assuming assertThrows only checks type, not message
- Confusing assertEquals order of arguments
- Thinking test fails if exception is thrown
assertThrows(IllegalStateException.class, () -> { throw new IllegalStateException("Error occurred"); }, "Error occurred");Solution
Step 1: Review assertThrows method signature
assertThrows expects (Class, Executable) or (Class, Executable, String) where String is failure message, not exception message.Step 2: Understand how to check exception message
Exception message must be checked separately after assertThrows returns the exception.Final Answer:
assertThrows does not accept a message as third argument for checking exception message -> Option DQuick Check:
Third argument in assertThrows is failure message, not exception message [OK]
- Passing expected exception message as third argument to assertThrows
- Confusing failure message with exception message
- Not capturing exception to check message separately
Solution
Step 1: Use assertThrows to capture the exception for input "bad"
assertThrows returns the exception object for further checks.Step 2: Assert the exception message matches expected "Bad input"
Use assertEquals on ex.getMessage() to verify the message.Final Answer:
var ex = assertThrows(IllegalArgumentException.class, () -> method("bad")); assertEquals("Bad input", ex.getMessage()); -> Option AQuick Check:
Capture exception then check message separately = D [OK]
- Passing message as third argument to assertThrows
- Using assertTrue incorrectly with lambda
- Not failing test if exception not thrown
Ready to prove it?
Pass the test and this pattern moves unknown
