首页 > 解决方案 > NetBeans Junit5 测试输出忽略 DisplayName 嵌套格式

问题描述

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

在此处输入图像描述

源代码

@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 中的测试显示显示 DisplayNames 并使用嵌套分组?

NetBeans 版本:12.0 Java 版本:11(OpenJDK)

标签: netbeansjunit5

解决方案


Maven在 surefire 插件版本@Display中添加了对注解的支持。此外,您需要配置参数和扩展名以允许 NetBeans 正确匹配显示名称。3.0.0.0-M4 statelessTestsetReporterstatelessTestsetInfoReporter

所以目前(01.2021),你可以使用 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 不支持在测试结果窗口中对测试结果进行嵌套分组。可能最好为此类测试使用不同的类而不是嵌套类。


推荐阅读