【发布时间】:2013-07-17 07:38:30
【问题描述】:
有一个关于 junit 的 ExpectedException 规则使用的问题:
这里建议:junit ExpectedException Rule 从 junit 4.7 开始,可以测试这样的异常(这比 @Test(expected=Exception.class) 要好得多):
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testFailuresOfClass() {
Foo foo = new Foo();
exception.expect(Exception.class);
foo.doStuff();
}
现在我需要在一个测试方法中测试几个异常,并在运行以下测试后得到一个绿色条,因此认为每个测试都通过了。
@Test
public void testFailuresOfClass() {
Foo foo = new Foo();
exception.expect(IndexOutOfBoundsException.class);
foo.doStuff();
//this is not tested anymore and if the first passes everything looks fine
exception.expect(NullPointerException.class);
foo.doStuff(null);
exception.expect(MyOwnException.class);
foo.doStuff(null,"");
exception.expect(DomainException.class);
foo.doOtherStuff();
}
但是过了一会儿,我意识到测试方法在第一次检查通过后就退出了。这至少可以说是模棱两可的。在junit 3中,这很容易实现...... 所以这是我的问题:
如何使用 ExpectedException Rule 在一个测试中测试多个异常?
【问题讨论】:
-
你仍然可以使用 Junit 3 的方式,但是不可能使用 @Test(expected) 和 ExpectedException。
-
还不错。通常最好每次测试都测试一件事。如果你在同一个测试中测试一个列表的大小和内容,比如 notNull,这是可以的,但不要在一个测试中测试两个概念上不同的东西。它的可读性较差。如果您每次测试测试一件事,您可以使用方法名称来描述您正在测试的内容,而不是“testException”
标签: java unit-testing junit junit4 expected-exception