您如何断言在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
相关文章
相关标签/搜索