问题:
How can I use JUnit4 idiomatically to test that some code throws an exception? 如何惯用JUnit4来测试某些代码引发异常?
While I can certainly do something like this: 虽然我当然可以做这样的事情:
@Test
public void testFooThrowsIndexOutOfBoundsException() {
boolean thrown = false;
try {
foo.doStuff();
} catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue(thrown);
}
I recall that there is an annotation or an Assert.xyz or something that is far less kludgy and far more in-the-spirit of JUnit for these sorts of situations. 我记得在这种情况下,有一个批注或一个Assert.xyz或一些不那么杂乱无章的东西 ,更是JUnit的精髓。
解决方案:
参考一: https://stackoom.com/question/eiF/您如何断言在JUnit-测试中引发了某种异常参考二: https://oldbug.net/q/eiF/How-do-you-assert-that-a-certain-exception-is-thrown-in-JUnit-4-tests
来源:oschina
链接:https://my.oschina.net/u/4438370/blog/4299505