我正在使用Junit5。我想实现一个动态测试,其中测试用例在YAML文件中定义。现在我作为标题遇到了问题。
查看我的示例代码:
@Test
public void t1() throws Exception {
java.lang.reflect.Method method = org.junit.jupiter.api.Assertions.class.getMethod("assertEquals", int.class, int.class);
method.invoke(null, 1, 5);
}
@TestFactory
Collection<DynamicTest> dynamicTests() {
DynamicTest[] list = new DynamicTest[1];
list[0] = DynamicTest.dynamicTest("dt1", new Executable() {
@Override
public void execute() throws Throwable {
java.lang.reflect.Method method = org.junit.jupiter.api.Assertions.class.getMethod("assertEquals", int.class, int.class);
method.invoke(null, 1, 50);
}
});
return Arrays.asList(list);
}
在示例代码中,我定义了静态测试用例t1和动态测试caes dt1。在这两个测试中,我只是做一个动态断言。
但是看看测试结果:
Failures (2):
JUnit Jupiter:TestRunner:t1()
MethodSource [className = 'com.mycompany.test.TestRunner', methodName = 't1', methodParameterTypes = '']
=> org.opentest4j.AssertionFailedError: expected: <1> but was: <5>
org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:52)
org.junit.jupiter.api.AssertEquals.failNotEqual (AssertEquals.java:197)
org.junit.jupiter.api.AssertEquals.assertEquals (AssertEquals.java:154)
org.junit.jupiter.api.AssertEquals.assertEquals (AssertEquals.java:149)
org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:305)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.mycompany.test.TestRunner.t1(TestRunner.java:82)
[...]
JUnit Jupiter:TestRunner:dynamicTests():dt1
MethodSource [className = 'com.mycompany.test.TestRunner', methodName = 'dynamicTests', methodParameterTypes = '']
=> java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.mycompany.test.TestRunner$1.execute(TestRunner.java:96)
org.junit.jupiter.engine.descriptor.JupiterTestDescriptor. executeAndMaskThrowable (JupiterTestDescriptor.java:141)
org.junit.jupiter.engine.descriptor.DynamicTestTestDescriptor. execute(DynamicTestTestDescriptor.java:41)
org.junit.jupiter.engine.descriptor.DynamicTestTestDescriptor. execute(DynamicTestTestDescriptor.java:24)
org.junit.platform.engine.support.hierarchical. HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3 (HierarchicalTestExecutor.java:112)
org.junit.platform.engine.support.hierarchical.SingleTestExecutor. executeSafely(SingleTestExecutor.java:66)
[...]
对于t1,断言正确显示为org.opentest4j.AssertionFailedError: expected: <1> but was: <5>
,但对于dt1,断言显示java.lang.reflect.InvocationTargetException
。有没有人看到同样的问题?如何将预期的断言结果作为t1用于dt1?
答案 0 :(得分:1)
检查InvocationTargetException
的原因。它应该读取以下内容:
Caused by: org.opentest4j.AssertionFailedError: expected: <1> but was: <50>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:52)
at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:154)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:149)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:305)
...
如果您认为这不是理想的行为,请在https://github.com/junit-team/junit5/issues
提交问题如果您使用dt1
替换Assertions.assertEquals(1, 50);
中的反思方法调用,则会按预期获得AssertionFailedError
。
答案 1 :(得分:0)
正如JUnit Jupiter的issue tracker中所讨论的,这个问题的解决方案不是编写自己的反射代码来调用Assertions
中的方法,而是简单地委托给ReflectionSupport
以确保InvocationTargetException
已妥善解开。
例如,以下两种情况都适用于您。
ReflectionSupport.invokeMethod(method, null, 1, 5);