有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单元测试java get测试注释

在我的项目(ReflectionProject)中,我搜索通用JAR(JarTest1.JAR)中所有类中的所有测试方法(搜索特定文件夹中的所有JAR,例如公共静态字符串pathJars=“C:\Users\g.Ventura\Desktop\test\u JAR”)

在项目JarTest1中:

如果在我的ReflectionProject中时测试方法位于测试文件夹(Junit Test)中,请执行以下操作:

Annotation annTest = method.getAnnotation(Test.class);

我始终获得annTest=null

如果测试方法位于src/main/java/my ReflectionProject中,则实际上可以正常工作:

Annotation annTest = method.getAnnotation(Test.class);
annTest != null..

对于带有测试文件夹的JarTest1包,我在pom处添加了一个。xml还提供了“maven assembly plugin”,用于获取2个jar:一个在测试文件夹中有类,另一个没有测试文件夹

为什么??反射类的getAnnotation方法不适用于测试文件夹

谢谢 问候


共 (1) 个答案

  1. # 1 楼答案

    我已决定参加考试。类仅用于junit5,但JarTest1是用JUNIT4编写的,因此:

    更改此行:

    Annotation annTest = method.getAnnotation(Test.class);
    

    与:

    Annotation annTestj4 = method.getAnnotation(org.junit.Test.class); //junit4
    Annotation annTestj5 = method.getAnnotation(org.junit.jupiter.api.Test.class);//junit5
    
    Annotation annTestFactory = method.getAnnotation(TestFactory.class);
    Annotation annTestDisabled = method.getAnnotation(Disabled.class);
    
    if ((annTestj4 != null || annTestFactory != null || annTestj5 != null) && annTestDisabled == null) {
        testMethods.add(method.getName());
    }