java - 使用 Java 反射 Junit4 测试异常

标签 java reflection junit4 invoke

我有一个返回异常的函数,我正在 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)

最佳答案

一种方法是再次抛出预期的错误,但是否推荐

 try
    {
        method.invoke(Hello.class, "random");
    }
    catch (InvocationTargetException e)
    {
        throw e.getTargetException();
    }

关于java - 使用 Java 反射 Junit4 测试异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29773319/

相关文章:

java - 如何 Hook 关闭端点,Spring Boot

java - 如何控制 selenium Web 驱动程序浏览器窗口

java - 对 .class 字段的反射(reflection)

java - assertEquals,什么是实际的,什么是预期的?

java - 如何为 mule 自定义变压器编写单元测试用例

java - 调用虚方法 double android.location.Location.getLatitude

java - 使用 Spring Data JpaRepositories 按 ElementCollections 中的值查询实体

c# - 使用反射编写 ORM 无法正确转换

java - getActualTypeArguments 与父级,从子级获取其参数化类型

Spring Boot JUnit 和 @TestPropertySource 使用多个属性文件