有一个关于 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 规则在一个测试中测试多个异常?