我有一个类,它的属性我不想为空。
setter 中的 Si 如下所示:
public void setFoo(String bar)
{
if (bar == null)
throw new IllegalArgumentException("Should not be null");
foo = bar;
}
在我的 JUnit 测试用例中,我想断言如果我执行 obj.setFoo(null),它将失败。 我怎样才能做到这一点?
最佳答案
JUnit4:
@Test(expected= IllegalArgumentException.class)
public void testNull() {
obj.setFoo(null);
}
JUnit3:
public void testNull() {
try {
obj.setFoo(null);
fail("IllegalArgumentException is expected");
} catch (IllegalArgumentException e) {
// OK
}
}
关于java - JUnit 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26914417/