使用Java反射和Junit4测试异常

6

我有一个函数返回一个异常,现在我要在Junit4中编写一个单元测试用例。问题是,reflect.invoke总是将异常包装在InvocationTargetException中,因此无法使用ExpectedException类检查异常。

public class Hello {
private void printHello(String msg) {
    if ("hello".equals(msg)) {
        System.out.println("Hello");
    }
        else throw new HeaderException();
    }
}

测试:

@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public void testPrint() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    expectedEx.expect(HeaderException.class);

    Method method = Hello.class.getDeclaredMethod("printHello", String.class);
    method.setAccessible(true);
    method.invoke(Hello.class,"random");

}

输出:

    java.lang.AssertionError: 
Expected: an instance of com.locationguru.CSF.exception.HeaderException
     got: <java.lang.reflect.InvocationTargetException>
 <Click to see difference>

    at org.junit.Assert.assertThat(Assert.java:780)
    at org.junit.Assert.assertThat(Assert.java:738)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:114)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

可能是 https://dev59.com/lnNA5IYBdhLWcg3wrP2q 的重复问题。 - Marvin
我强烈反对像这样测试私有方法。如果您需要它可见但不是太明显,请考虑将其设置为包私有。 - Makoto
起初我以为 expectedEx.expectCause(instanceOf(HeaderException.class)); 就可以了,但不幸的是 HeaderException 并没有存储在 cause 中,而是存储在 target 中。我建议你编写自己的 Matcher 或在网络上搜索通用的 ExceptionMatcher。 - CoronA
1个回答

7
一种做法可能是再次抛出预期的错误,但这是否推荐呢?
 try
    {
        method.invoke(Hello.class, "random");
    }
    catch (InvocationTargetException e)
    {
        throw e.getTargetException();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接