在JUnit测试用例中'fail‘的实际用法是什么?
发布于 2010-10-06 14:28:34
假设您正在为负流编写一个测试用例,其中被测试的代码应该会引发异常。
try{
bizMethod(badData);
fail(); // FAIL when no exception is thrown
} catch (BizException e) {
assert(e.errorCode == THE_ERROR_CODE_U_R_LOOKING_FOR)
}
发布于 2010-10-06 14:27:28
我认为通常的用例是在负测试中没有抛出异常时调用它。
类似于下面的伪代码:
test_addNilThrowsNullPointerException()
{
try {
foo.add(NIL); // we expect a NullPointerException here
fail("No NullPointerException"); // cause the test to fail if we reach this
} catch (NullNullPointerException e) {
// OK got the expected exception
}
}
发布于 2014-08-22 04:06:54
我已经在我的@‘ve方法中出现错误的情况下使用了它。
public Object obj;
@Before
public void setUp() {
// Do some set up
obj = new Object();
}
@Test
public void testObjectManipulation() {
if(obj == null) {
fail("obj should not be null");
}
// Do some other valuable testing
}
https://stackoverflow.com/questions/3869954
复制相似问题