kup*_*fic 4 java junit unit-testing
我正在编写测试以验证我的方法的合同,例如,参数不能null,int参数必须是正的等等.当违反合同时,抛出的异常类型反映了它失败的原因,例如NullPointerException或IllegalArgumentException.有时我不关心这两个中的哪一个被抛出,只有其中一个被抛出.(是的,我应该测试确切的异常类型,但请耐心等待...)
我可以使用JUnit @Test(expected = RuntimeException.class)(因为RuntimeException它是最接近的公共父IAE和NPE),但这是过于通用的,因为被测试的方法可能会引发其他一些RuntimeException.
相反,我正在尝试使用JUnit的ExpectedExceptions类.
这是一个完整的例子:
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.CoreMatchers.anyOf;
public class ParameterViolationTest {
private Target target = new Target();
@Rule
public ExpectedException expected = ExpectedException.none();
@Test
public void parameterViolation() {
expected.expect(anyOf(
CoreMatchers.<Class<? extends Exception>>
equalTo(NullPointerException.class),
CoreMatchers.<Class<? extends Exception>>
equalTo(IllegalArgumentException.class)));
// Null parameter violates contract, throws some exception.
target.methodUnderTest(null);
}
private static class Target {
public void methodUnderTest(Object o) {
if (o == null) {
// throw new IllegalArgumentException();
throw new NullPointerException();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这个测试能够通过,但它会失败:
java.lang.AssertionError:
Expected: (<class java.lang.NullPointerException> or <class java.lang.IllegalArgumentException>)
but: was <java.lang.NullPointerException>
Stacktrace was: java.lang.NullPointerException
at ParameterViolationTest$Target.methodUnderTest(ParameterViolationTest.java:35)
at ParameterViolationTest.parameterViolation(ParameterViolationTest.java:25)
< internal calls... >
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
< internal calls... >
java.lang.AssertionError:
Expected: (<class java.lang.NullPointerException> or <class java.lang.IllegalArgumentException>)
but: was <java.lang.NullPointerException>
Stacktrace was: java.lang.NullPointerException
at ParameterViolationTest$Target.methodUnderTest(ParameterViolationTest.java:35)
at ParameterViolationTest.parameterViolation(ParameterViolationTest.java:25)
< internal calls... >
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
< internal calls... >
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:865)
at org.junit.Assert.assertThat(Assert.java:832)
at org.junit.rules.ExpectedException.handleException(ExpectedException.java:198)
at org.junit.rules.ExpectedException.access$500(ExpectedException.java:85)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:177)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
< internal calls... >
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
你必须使用Matchers.instanceOf而不是CoreMatchers.equalTo,因为你正在比较的实例XXXException.
expected.expect(anyOf(
instanceOf(NullPointerException.class),
instanceOf(IllegalArgumentException.class)));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2898 次 |
| 最近记录: |