我的测试功能是:
public void functionToTest(MetricConfig flinkMetricsConfig) {
try {
this.initializeFunction(inputs);
} catch (Exception ex) {
throw new RuntimeException("Trouble configuring ", ex);
}
}
内部this.initializeFunction(...)
对其中一个类变量进行了验证检查,并提供NullpointerException
。 RuntimeException
与Trouble configuring message
一起发现了异常。
在我的单元测试中,我正在测试类变量的验证检查失败的情况。
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testWithFailedValidation() {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Trouble configuring... ");
functionToTest();
}
我收到此错误:
java.lang.AssertionError:
Expected: an instance of java.lang.RuntimeException
but: <org.junit.internal.runners.model.MultipleFailureException: There were 2 errors:
java.lang.RuntimeException(Trouble configuring... )
java.lang.NullPointerException(null)> is a org.junit.internal.runners.model.MultipleFailureException
Stacktrace was: org.junit.internal.runners.model.MultipleFailureException: There were 2 errors:
java.lang.RuntimeException(Trouble configuring... )
java.lang.NullPointerException(null)
at junit internals blabla stacktrace...
-----------------------更新------------------ 嗨,我把测试改为:
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testWithFailedValidation() {
expectedException.expect(MultipleFailureException.class);
expectedException.expectMessage("Trouble configuring... ");
functionToTest();
}
测试通过。但我想确保MultipleFailureException
包含RuntimeException和NullException。有办法检查那些吗?我正在使用junit 4.11。谢谢。
------------更新------------------- 我使用try catch(Error1){assertbehaviour1} catch(错误2){assert behaviour2}进行测试。