This question already has answers here:
JUnit Testing Exceptions [duplicate]
(5个答案)
5年前关闭。
我知道我的程序的一部分会在尝试测试时失败,并且在测试失败时我不会通过测试。所以我想知道是否有可能断言失败?
如果您使用的是Junit 3,则必须赶上它
(5个答案)
5年前关闭。
我知道我的程序的一部分会在尝试测试时失败,并且在测试失败时我不会通过测试。所以我想知道是否有可能断言失败?
最佳答案
如果您使用的是Junit 4,请使用预期的行为:
@Test(expected=SomeException.class)
public void testThings() {
// do stuff
}
如果您使用的是Junit 3,则必须赶上它
public void test() {
try {
// do stuff
fail("Expected SomeException");
} catch (SomeException e) {
}
}