How do I assert my exception message with JUnit Test annotation?
You could use the @Rule
annotation with ExpectedException
, like this:
@Rulepublic ExpectedException expectedEx = ExpectedException.none();@Testpublic void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception { expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Employee ID is null"); // do something that should throw the exception... System.out.println("=======Starting Exception process======="); throw new NullPointerException("Employee ID is null");}
Note that the example in the ExpectedException
docs is (currently) wrong - there's no public constructor, so you have to use ExpectedException.none()
.
In JUnit 4.13 you can do:
import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertThrows;...@Testvoid exceptionTesting() { IllegalArgumentException exception = assertThrows( IllegalArgumentException.class, () -> { throw new IllegalArgumentException("a message"); } ); assertEquals("a message", exception.getMessage());}
This also works in JUnit 5 but with different imports:
import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertThrows;...
I like the @Rule
answer. However, if for some reason you don't want to use rules. There is a third option.
@Test (expected = RuntimeException.class)public void myTestMethod(){ try { //Run exception throwing operation here } catch(RuntimeException re) { String message = "Employee ID is null"; assertEquals(message, re.getMessage()); throw re; } fail("Employee Id Null exception did not throw!"); }