NetBeans Junit5测试输出忽略DisplayName嵌套格式

uqxowvwt  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(157)

在NetBeans中,我没有从标注“@DisplayName”中获取名称作为测试结果。只显示测试函数的名称。嵌套测试的分组显示也被忽略。

源代码

@DisplayName("test facade")
public class TestFacadeTest {

    @BeforeAll
    public static void setUpClass() {
    }

    @AfterAll
    public static void tearDownClass() {
    }

    @BeforeEach
    public void setUp() {
    }

    @AfterEach
    public void tearDown() {
    }

    @Test
    @DisplayName("senseless test")
    public void test(){
        Assertions.assertTrue(true);
    }

    @Nested
    @DisplayName("tests - compareStringNullSave")
    class CompateStringNullSaveTestGroup {

        @BeforeEach
        public void setUp() { 
        }

        @Test
        @DisplayName("both identical")
        public void Test1(){
            String str1 = "Test123";
            String str2 = "Test123";
            Assertions.assertTrue(TestFacade.compareStringNullSave(str1, str2));
            Assertions.assertTrue(TestFacade.compareStringNullSave(str2, str1));
        }

        @Test
        @DisplayName("Identical text but different uppercase and lowercase letters")
        public void Test2(){
            String str1 = "Test123";
            String str2 = "test123";
            Assertions.assertFalse(TestFacade.compareStringNullSave(str1, str2));
            Assertions.assertFalse(TestFacade.compareStringNullSave(str2, str1));
        }
    } 
}

从pom.xml中提取

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

<build>
    <plugins>
        [...]
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <properties>
                    <configurationParameters>
                        junit.jupiter.conditions.deactivate = *
                        junit.jupiter.extensions.autodetection.enabled = true
                        junit.jupiter.testinstance.lifecycle.default = per_class
                    </configurationParameters>
                </properties>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

我必须进行什么设置才能使NetBeans中的测试显示显示DisplayName并使用嵌套分组?
NetBeans版本:12.0 Java版本:第11页(开放式JDK)

nkkqxpd9

nkkqxpd91#

Maven在surefire插件的3.0.0.0-M4版本中添加了对@Display注解的支持。此外,您需要配置statelessTestsetReporterstatelessTestsetInfoReporter扩展的参数,以允许NetBeans正确匹配显示名称。
因此,目前(2021年1月),您可以使用3.0.0.0-M4或更高版本的surefire插件,JUnit的版本应该高于5.5.2。

<plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
                    <version>3.0</version>
                    <usePhrasedFileName>true</usePhrasedFileName>
                    <usePhrasedTestSuiteClassName>true</usePhrasedTestSuiteClassName>
                    <usePhrasedTestCaseClassName>true</usePhrasedTestCaseClassName>
                    <usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
                </statelessTestsetReporter>
                <statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
                    <usePhrasedClassNameInRunning>true</usePhrasedClassNameInRunning>
                </statelessTestsetInfoReporter>
                 <properties>
                    <configurationParameters>
                       junit.jupiter.conditions.deactivate = *
                       junit.jupiter.extensions.autodetection.enabled = true
                       junit.jupiter.testinstance.lifecycle.default = per_class
                    </configurationParameters>
                </properties>
            </configuration>                                
        </plugin>
    </plugins>

下面是我的案例:

@DisplayName("Display Name of class")
public class testClassSimple {

    @Test
    @DisplayName("Display name of method 1")
    public void testMethod1() {
    }

    @Test
    @DisplayName("Display name of method 2")
    public void testMethod2() {
    }
}

对于嵌套分组和@Nested注解,NetBeans目前还不支持在测试结果窗口中对测试结果进行嵌套分组。对于这样的测试,使用不同的类可能比使用嵌套类更好。

相关问题