我正在使用 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 和一个动态测试用例 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
。有人看到同样的问题吗?如何获得 dt1 的预期断言结果为 t1?
最佳答案
检查InitationTargetException
的原因。它应该读取如下内容:
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
作为预计。
关于java - Junit5:动态测试中动态断言工作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113936/