📜  如何断言抛出异常java代码示例

📅  最后修改于: 2022-03-11 14:52:23.896000             🧑  作者: Mango

代码示例1
@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));
}