繁体   English   中英

JUnit的ExpectedException无法期望多个异常

[英]Unable to expect multiple exceptions with JUnit's ExpectedException

  • Java 7
  • JUnit 4.11

我正在编写测试以验证我的方法的合同,例如,参数不能为nullint参数必须为正,等等。当违反合同时,抛出的异常类型将反映其失败的原因,例如NullPointerExceptionIllegalArgumentException 有时我不在乎这两个中的哪一个被抛出,仅是其中之一被抛出。 (是的,我应该测试确切的异常类型,但是请忍受...)

我可以使用JUnit的@Test(expected = RuntimeException.class) (因为RuntimeExceptionIAENPE的最接近的公共父级),但是由于测试中的方法可能会抛出其他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();
            }
        }
    }
}

我希望该测试能够通过,但是失败了:

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... >

这里发生了什么?

你必须使用Matchers.instanceOf代替CoreMatchers.equalTo ,因为你比较的情况下XXXException

expected.expect(anyOf(
  instanceOf(NullPointerException.class),
  instanceOf(IllegalArgumentException.class)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM