我有一个问题ExpectedException
。我在一个测试类的许多测试中使用它。现在我遇到了一个问题,我们的 Jenkins 有一个失败的测试,因为这个测试有expectedMessage
一个之前的测试。我不知道为什么,但为了防止这种失败,我试图“清除”ExpectedMessage
但它的行为不像预期的那样:
测试类
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。