该如何使用 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