【发布时间】:2012-07-11 05:25:11
【问题描述】:
我有代码可以检查 jUnit 中的异常。我想知道以下哪一项是好的 jUnit 实践?
第一
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void checkNullObject() throws CustomException {
exception.expect(CustomException.class);
MyClass myClass= null;
MyCustomClass.get(null);
}
第二
@Test(expected=CustomException.class)
public void checkNullObject() throws CustomException {
MyClass myClass= null;
MyCustomClass.get(null);
}
编辑:请注意 CustomException 是未经检查的自定义异常。 (虽然不会对这个问题产生任何影响)。
【问题讨论】:
-
stackoverflow.com/questions/785618/… 的副本或主要基于意见