how to assert an exception in java junit test

https://w‮ual.ww‬tturi.com
how to assert an exception in java junit test
@Test
public void whenExceptionThrown_thenAssertionSucceeds() {
    Exception exception = assertThrows(NumberFormatException.class, () -> {
        Integer.parseInt("1a");
    });

    String expectedMessage = "For input string";
    String actualMessage = exception.getMessage();

    assertTrue(actualMessage.contains(expectedMessage));
}
Created Time:2017-09-09 22:19:09  Author:lautturi