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