您如何斷言在JUnit 4測試中引起了某種異常? - How do you assert that a certain exception is thrown in JUnit 4 tests?

問題:

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


解決方案:

參考一: 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
相關文章
相關標籤/搜索