对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规则在一个测试中测试多个异常?
解决方法
简短的回答:你做不到.
如果第一次调用–foo.doStuff() – 抛出异常,你永远不会到达foo.doStuff(null).你必须把你的测试分成几个(对于这个简单的情况,我建议回到简单的符号,没有ExpectedException):
private Foo foo; @Before public void setUp() { foo = new Foo(); } @Test(expected = IndexOutOfBoundsException.class) public void noArgsShouldFail() { foo.doStuff(); } @Test(expected = NullPointerException.class) public void nullArgShouldFail() { foo.doStuff(null); } @Test(expected = MyOwnException.class) public void nullAndEmptyStringShouldFail() { foo.doStuff(null,""); } @Test(expected = DomainException.class) public void doOtherStuffShouldFail() { foo.doOtherStuff(); }
如果你真的想要一个且只有一个测试,如果没有抛出错误你就会失败,并抓住你期望的东西:
@Test public void testFailuresOfClass() { Foo foo = new Foo(); try { foo.doStuff(); fail("doStuff() should not have succeeded"); } catch (IndexOutOfBoundsException expected) { // This is what we want. } try { foo.doStuff(null); fail("doStuff(null) should not have succeeded"); } catch (NullPointerException expected) { // This is what we want. } // etc for other failure modes }
但是,这很快就会变得非常混乱,如果第一个期望失败,你就不会看到其他任何事情也会失败,这在排除故障时会很烦人.