簡體   English   中英

Junit5:動態斷言在動態測試中工作異常

[英]Junit5: dynamic assert works abnormally in dynamic test

我正在使用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,assert正確顯示為org.opentest4j.AssertionFailedError: expected: <1> but was: <5> ,但是對於dt1,assert顯示java.lang.reflect.InvocationTargetException 有人看到過同樣的問題嗎? 如何獲得預期的斷言結果作為dt1的t1?

檢查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提交問題

如果您用Assertions.assertEquals(1, 50); dt1 Assertions.assertEquals(1, 50);替換dt1反射方法調用Assertions.assertEquals(1, 50); 您將按預期獲得AssertionFailedError

正如JUnit Jupiter的問題跟蹤器中所討論的那樣,解決此問題的方法不是編寫自己的反射代碼來調用Assertions方法,而是簡單地委派給ReflectionSupport以確保正確解開InvocationTargetException

例如,以下兩種情況均適用。

ReflectionSupport.invokeMethod(method, null, 1, 5);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM