Gangmax Blog

自由之思想,独立之精神

How to Assert Exception Message in JUnit?

| Comments

From here, here and here.

1
2
3
4
5
6
7
8
9
@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Employee ID is null");
    // do something that should throw the exception...
}

Comments