ExpectedException的重置不能按预期工作

时间:2017-06-13 10:06:17

标签: java junit4 expected-exception

我对ExpectedException有疑问。我在一个测试类的许多测试中使用它。现在我遇到的问题是我们的Jenkins有一个失败的测试,因为这个测试有一个先前测试的expectedMessage。我不知道为什么,但为了防止这种失败,我试图"清除" ExpectedMessage但它的行为与预期不符:

识别TestClass

public class TestClass {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void testExpectedException() throws Exception {
        //old expectation
        expectedException.expect(Exception.class);
        expectedException.expectMessage("something");

        //try to reset
        expectedException = ExpectedException.none();

        //new expectation
        expectedException.expect(Exception.class);
        expectedException.expectMessage("random");
        Thrower exe = new Thrower();
        exe.testedMethod();
    }
}

运动员

public class Thrower {

    public void testedMethod() throws Exception {
        throw new Exception("random");
    }
}

testExpectedException导致以下输出:

java.lang.AssertionError: 
Expected: (an instance of java.lang.Exception and exception with message a string containing "something")
     but: exception with message a string containing "something" message was "random"
Stacktrace was: java.lang.Exception: random

但我希望expectedException检查包含random的邮件。为什么不呢?

我正在使用JUnit 4.11。

0 个答案:

没有答案
相关问题