我有如下创建的自定义Annotation

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestConfig {
    String[] value();
}


我进行了扩展BaseClass的测试。

import org.testng.annotations.Test;

public class MyTest extends BaseClass {

    @Test
    @TestConfig({ "enableCookies" })
    public void startTest() {
        startInstance();
    }
}


现在我需要在下面的@TestConfig中访问BaseClass批注内的值

导入org.testng.annotations.BeforeSuite;

public class BaseClass {

    public void startInstance() {
        System.out.println("starting instance");
        //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    }

    @BeforeSuite
    public void runChecks() {
        System.out.println("Checks done....");
    }
}


我知道我可以做TestConfig config = method.getAnnotation(TestConfig.class),但是如何访问TestNG TestMethod类?请帮忙。

最佳答案

您可以执行类似的操作(但在测试方法中删除直接调用):

@BeforeMethod
public void startInstance(Method m) {
    System.out.println("starting instance");
    //I need to access the value supplied in "MyTest" inside @TestConfig annotation here. How do I do that.
    TestConfig tc = m.getAnnotation(TestConfig.class);
    System.out.println(tc.value());
}

10-08 12:37