How can I test several exceptions within one test using an ExpectedException Rule?
对junit的ExpectedException规则的使用有疑问:
正如这里建议的那样:junit ExpectedException Rule
从junit 4.7开始,可以测试这样的异常(这比@Test(expected = Exception.class)要好得多):
1 2 3 4 5 6 7 8 9 | @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testFailuresOfClass() { Foo foo = new Foo(); exception.expect(Exception.class); foo.doStuff(); } |
现在我需要在一个测试方法中测试几个异常,并在运行以下测试后得到一个绿色条,因此认为每个测试都通过了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @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规则在一个测试中测试多个异常?
简答:你做不到。
如果第一次调用 -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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(); } |
如果你真的想要一个且只有一个测试,如果没有抛出错误,你可以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @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 } |
但是,这很快就会变得非常混乱,如果第一个期望失败,你将看不到其他任何事情也会失败,这在排除故障时会很烦人。