如何以惯用方式使用JUnit4来测试某些代码是否会抛出异常?
虽然我当然可以这样做:
@Test
public void testFooThrowsIndexOutOfBoundsException() {
boolean thrown = false;
try {
foo.doStuff();
} catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue(thrown);
}
Run Code Online (Sandbox Code Playgroud)
我记得有一个注释或一个Assert.xyz或者其他东西,对于这些类型的情况来说,远不如KUndgy 和JUnit的精神.
在JUnit中,我目前正在使用注释来期待我的测试中的异常.
有没有办法分析这个例外?例如,我期待a CriticalServerException,但我也想验证getMessage方法的内容.