java - 无法预期 JUnit 的 ExpectedException 出现多个异常

标签 java unit-testing junit

  • Java 7
  • JUnit 4.11

我正在编写测试来验证我的方法的契约,例如,参数不能为 nullint 参数必须为正,等等。当契约是违反时,抛出的异常类型反射(reflect)了它失败的原因,例如 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)));

关于java - 无法预期 JUnit 的 ExpectedException 出现多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20568489/

相关文章:

javascript - 用 sinon stub uuid

c# - 单元测试项目是否可以加载目标应用程序的 app.config 文件?

java - 运行 JUnit 测试忽略模块路径

java - 找不到 bean : “studentList” in any scope

Java使用反射调用类的main()方法

java - "Couldn' t 解析对语法 'org.eclipse.xtext.common.Terminals' 的引用“- PDE 已安装

AndroidX : No instrumentation registered! 必须在注册工具下运行

java - 如何运行给定包的所有 JUnit 测试?

java - 如何使用 EasyMock 测试 void 方法

java - Dagger 2.11 : Local Singleton when using @ContributesAndroidInjector