Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试NG注释扩展_Java_Testing_Testng - Fatal编程技术网

Java 测试NG注释扩展

Java 测试NG注释扩展,java,testing,testng,Java,Testing,Testng,我在项目中有一些带有注释的不稳定测试,比如“@TestretryAnalyzer=RetryAnalyzer.class”runner-TestNG。在RetryAnalyzer类中实现了重试逻辑 如何创建将扩展@Test annotation并具有默认值retryAnalyzer的自定义注释@TestretryAnalyzer=RetryAnalyzer.class->@UnstableTest 谢谢。这里有一个简单的方法 在您的案例中创建自定义注释@UnstableTest 构建org.te

我在项目中有一些带有注释的不稳定测试,比如“@TestretryAnalyzer=RetryAnalyzer.class”runner-TestNG。在RetryAnalyzer类中实现了重试逻辑

如何创建将扩展@Test annotation并具有默认值retryAnalyzer的自定义注释@TestretryAnalyzer=RetryAnalyzer.class->@UnstableTest


谢谢。

这里有一个简单的方法

在您的案例中创建自定义注释@UnstableTest 构建org.testng.IAnnotationTransformer的实现,在其中测试作为transform方法参数引入的Method对象,查看它是否有注释,如果有,则注入注释。 下面是它的外观:

标记片状测试的标记注释

注释转换器

现在使用标记添加此侦听器

这样就可以了

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
@interface UnstableTest {}
public static class UnstableTestInjector implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        if (testMethod == null) {
            return;
        }

        UnstableTest unstableTest = testMethod.getAnnotation(UnstableTest.class);
        if (unstableTest == null) {
            return;
        }
        annotation.setRetryAnalyzer(TryAgain.class);
    }
}