如何在 junit4测试中抛出某个异常?
该如何使用 JUnit4来测试某些代码抛出的异常?
我当然可以这样做:
@Test
public void testFooThrowsIndexOutOfBoundsException() {
boolean thrown = false;
try {
foo.doStuff();
} catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue(thrown);
}
我记得有一个注释或者 Assert.xyz 之类的东西,对于这类情况来说,它远没有那么复杂,而是更符合 JUnit 的精神。
转载于:https://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests
perhaps?
2008/10/01 06:56- exception
- junit4
- junit
- java
- assert
- 点赞
- 收藏
- 回答
满意答案
30个回复
