Checking exception message in JUnit - Build an Automation Script
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class NumberParserTest { // Method under test public int parseNumber(String input) { try { return Integer.parseInt(input); } catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid number format"); } } @Test void testParseNumberThrowsExceptionWithCorrectMessage() { IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { parseNumber("abc"); }); assertEquals("Invalid number format", exception.getMessage()); } }
The test method testParseNumberThrowsExceptionWithCorrectMessage uses assertThrows to check that calling parseNumber("abc") throws an IllegalArgumentException. The thrown exception is captured in the exception variable. Then assertEquals verifies the exception message matches exactly "Invalid number format". This approach avoids manual try-catch blocks and clearly expresses the intent to check both the exception type and its message.
The parseNumber method simulates the behavior that throws the exception with the expected message when input is invalid.
Now add data-driven testing with 3 different invalid inputs to verify the exception message each 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
