sle*_*ske 128
我发现它有用的一些情况:
Run Code Online (Sandbox Code Playgroud)try{ // do stuff... fail("Exception not thrown"); }catch(Exception e){ assertTrue(e.hasSomeFlag()); }
注意:
从JUnit4开始,有一种更优雅的方法来测试抛出异常:使用注释 @Test(expected=IndexOutOfBoundsException.class)
但是,如果您还想检查异常,那么这将不起作用,那么您仍然需要fail()
.
kar*_*eek 11
假设您正在为-ve flow编写测试用例,其中被测试的代码应引发异常
try{
bizMethod(badData);
fail(); // FAIL when no exception is thrown
} catch (BizException e) {
assert(e.errorCode == THE_ERROR_CODE_U_R_LOOKING_FOR)
}
Run Code Online (Sandbox Code Playgroud)
我认为通常的用例是在负面测试中没有抛出异常时调用它.
像下面的伪代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在我的@Before方法中出现问题的情况下使用过它.
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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
134482 次 |
最近记录: |