testLogicalDoc = new LogicalDocumentImpl(-4);
assertTrue(testLogicalDoc==null);
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我有一个断言条件,我想确保我不创建负大小的对象.它是封面下面的一个stringBuilder,它抛出NegativeArrayBoundsException,大小小于零.但我的junit测试在这里失败了.我不知道是否有任何其他方法可以确保没有使用负尺寸创建对象.有关如何测试它的任何想法?还是应该是Junit测试?
非常感谢,
-Pan
编辑:
@Test(expected=NegativeArraySizeException.class)
public void testCreate4b()
{
LogicalDocumentImpl testLogicalDoc = new LogicalDocumentImpl(-4);
}
Run Code Online (Sandbox Code Playgroud)
我正在LogicalDocumentImpl类中捕获异常,但仍然这个测试失败并出现断言错误,但只有当我尝试捕获断言错误时才成功..为什么这样?
如果你扔NegativeArrayBoundsException你的测试用例可以这样检查
@Test(expected= NegativeArrayBoundsException.class)
Run Code Online (Sandbox Code Playgroud)
这意味着你的测试应抛出异常NegativeArrayBoundsException.
或者你可以使用 fail('should never come here for negative values..')
testLogicalDoc = new LogicalDocumentImpl(-4);
fail('should never come here for negative values..');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |