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的精髓。)
ask by SCdF translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…