How can I use JUnit4 idiomatically to test that some code throws an exception? 如何惯用JUnit4来测试某些代码引起异常? less
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的精髓。 this