如何使用Junit 5和java在控制台中打印CustomDisplayNameGenerator值?

ftf50wuq  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(124)

我想使用junit 5和java在控制台中打印CustomDisplayNameGenerator值。

import org.junit.jupiter.api.DisplayNameGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

public class CustomDisplayNameGenerator extends DisplayNameGenerator.Standard {
    private static final Logger logger = LoggerFactory.getLogger(CustomDisplayNameGenerator.class);

    @Override
    public String generateDisplayNameForClass(Class<?> testClass) {
        return replaceCamelCase(super.generateDisplayNameForClass(testClass));
    }

    @Override
    public String generateDisplayNameForNestedClass(Class<?> nestedClass) {
        return replaceCamelCase(super.generateDisplayNameForNestedClass(nestedClass));
    }

    @Override
    public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {

        StringBuilder result = new StringBuilder();
        result.append( "Test" + ";");
        result.append( "Project" + "Test;");
        result.append( "JIRA id" + "435345;");
        return result.toString();

    }

    String replaceCamelCase(String camelCase) {
        StringBuilder result = new StringBuilder();
        result.append(camelCase.charAt(0));
        for (int i = 1; i < camelCase.length(); i++) {
            if (Character.isUpperCase(camelCase.charAt(i))) {
                result.append(' ');
                result.append(Character.toLowerCase(camelCase.charAt(i)));
            } else {
                result.append(camelCase.charAt(i));
            }
        }
        return result.toString();
    }
}

我不知道它是什么,我需要做进一步.有人可以帮助我请?这是代码到目前为止,我已经得到:

import Framework.Annotations.Common.*;
import org.junit.jupiter.api.*;
import java.lang.annotation.Annotation;
import static org.junit.jupiter.api.DisplayNameGenerator.getDisplayNameGenerator;

@DisplayNameGeneration(CustomDisplayNameGenerator.class)
public class JUnit5GeneratedExample  {

    @Test
    void simpleTestExecution() {
        Annotation displayNameGeneration = getClass().getAnnotation(DisplayNameGeneration.class);
        if (displayNameGeneration != null) {
            System.out.println(displayNameGeneration.toString());
            DisplayNameGenerator name = getDisplayNameGenerator(DisplayNameGenerator.Standard.class);
            System.out.println(name.generateDisplayNameForMethod(CustomDisplayNameGenerator.class,  new Object(){}.getClass().getEnclosingMethod()));
        }
        System.out.printf("This is printed");
    }
}

我正在尝试在控制台中打印下面突出显示的文本。是否有方法在控制台中打印CustomDisplayNameGenerator?

iszxjhcz

iszxjhcz1#

代码看起来不错,只有一个小问题:

DisplayNameGenerator name = getDisplayNameGenerator(DisplayNameGenerator.Standard.class);

这一行将返回DisplayNameGenerator.Standard的一个示例,所以在你的例子中,你需要在这里使用你自己的类:

DisplayNameGenerator nameGenerator = DisplayNameGenerator.getDisplayNameGenerator(CustomDisplayNameGenerator.class);
System.out.println(nameGenerator.generateDisplayNameForMethod(CustomDisplayNameGenerator.class,  new Object(){}.getClass().getEnclosingMethod()));

另一方面,这与直接调用构造函数没有太大区别:

DisplayNameGenerator nameGenerator = new CustomDisplayNameGenerator();
System.out.println(nameGenerator.generateDisplayNameForMethod(CustomDisplayNameGenerator.class,  new Object(){}.getClass().getEnclosingMethod()));

正如在评论中所讨论的,我找到了一个更好的解决方案:您可以定义一个自定义测试扩展来实现 BeforeTestExecutionCallback

public class PrintTestNameBeforeEach implements BeforeTestExecutionCallback {
    @Override
    public void beforeTestExecution(ExtensionContext context) {
        System.out.println(context.getDisplayName());
    }
}

现在,您可以简单地使用@ExtendWith(PrintTestNameBeforeEach.class)注解任何测试类,并且所有方法都将被打印出来,如果您有自定义显示名称生成器的话。

@ExtendWith(PrintTestNameBeforeEach.class)
@DisplayNameGeneration(CustomDisplayNameGenerator.class)
class JUnit5Callbacks {

    @Test
    void myFirstTest() {
        System.out.println("running the first test...");
    }

    @Test
    void mySecondTest() {
        System.out.println("running the second test...");
    }
}

Ps:我写了一篇关于它的小文章,你可以在这里看到所有的步骤:https://medium.com/javarevisited/junit5-callbacks-in-less-than-3-minutes-55a87e000fcc

相关问题