我对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。