注释变换器调用

时间:2015-03-03 14:18:41

标签: java testng

我想在阅读参考资料后在我的代码中使用注释变换器。我仍然不完全了解如何以编程方式调用注释转换器到我的orignal代码(我在http://testng.org/doc/documentation-main.html之前阅读它)。假设我有以下代码:

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Test;



public class TestNGTest {

   @Test(dependsOnMethods = { "testCase2" })
   public void testCase1() {
      System.out.println("in test case 1");
   }

   @Test
   public void testCase2() {
      System.out.println("in test case 2");
   }



   public static class MyTransformer implements IAnnotationTransformer {

        @Override
        public void transform(final ITestAnnotation annotation, final Class testClass,
                final Constructor testConstructor, final Method testMethod) {
           //some transformations of my annotation
        }


   }
}

如何以编程方式调用“transform”? 非常感谢您提前帮助 你介意给我一些简单的示例代码吗? 非常感谢你提前。

1 个答案:

答案 0 :(得分:0)

非常感谢阅读这篇文章的人。 最后,我明白了问题是什么,答案很简单:

@Test
public void verifyAnnotationWithTransformer() {
    TestNG tng = new TestNG();
    MyTransformer myTransformer = new MyTransformer();
    tng.setTestClasses(new Class[] {TestNGTest.class});
    tng.setAnnotationTransformer(myTransformer);
    tng.run();
}

该点是第二行,即将annoation变换器的整个对象放在函数setAnnotationTransformer中。我之前没有注意到这样的功能。我很蠢。 = _ =” 再次感谢那些可能尝试提供帮助的人。