对于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();
}