我有如下创建的自定义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());
}