如何断言JUnit测试中抛出了某个异常
fullstacker 发布于 2021-03-20

对于junit5.x,可以使用assertThrows,如下所示

 @Test
public void testThrowsNullPointerException() {
    Throwable exception = assertThrows(NullPointerException.class, () -> test.Func1());
    assertEquals("expected messages", exception.getMessage());
}
在JUnit4.x中对于junit4.x,使用可选的Test Annonation的'expected'属性

 @Test(expected = NullPointerException.class)
public void testNullPointerException() {
    test.Func1();
}

全栈者
关注 私信
文章
31
关注
0
粉丝
0