Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use IAnnotationTransformer in testNG?

How do I use IAnnotationTransformer in testNG? The code never stepped into the transform function when I debugged. It executed all 3 tests. I use Maven to trigger my tests, this is what I have--

public class SomeTest {

    @BeforeClass
    public void before(){

        TestNG testNG = new TestNG();
        testNG.setAnnotationTransformer(new Transformer());
    }

    @Test(priority = 1)
    public void test1(){}

    @Test(priority = 2)
    public void test2(){}

    @Test(priority = 3)
    public void test3(){}
}

public class Transformer implements IAnnotationTransformer {

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){

        if (true){
            annotation.setEnabled(false);
        }
    }
}
like image 752
connect2krish Avatar asked Nov 11 '22 04:11

connect2krish


1 Answers

Listeners are not automatically picked up until you have set them up using serviceloaders.

There are multiple ways to specify a listener, based on need.
If you do not want to apply them to all your tests, you may choose to apply a listener on a single class

eg.

@Listeners(Transformer.class)
public class InterceptorTC {

@Test(priority..

You can also specify a listener from maven surefire plugin. Read the using custom listener section here

Other ways to specify listener can be to define it in your suite xml and specify your suite xml file in the maven plugin.

.

like image 72
niharika_neo Avatar answered Nov 15 '22 12:11

niharika_neo