White Box Testing
Test your code
   Home      TestNG Annotation Transformer
 

TestNG Annotation Transformer

Annotation Transformer used to override annotations behavior at runtime. There may be situation to run your some tests with some different annotations

Implement below interface for annotation transformer

public interface IAnnotationTransformer {

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

}

 

For concurrencyTest which runs with threadPoolSize 3

@Test(threadPoolSize = 3, invocationCount = 20)

public void concurrencyTest() {

       System.out.print(" " + Thread.currentThread().getId());

}

 

In some other scenario we want to run it with threadPoolSize 5. Written below class CalcAnnoTransformer.java

package testNGAnnoTransformer;

 

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

 

import org.testng.IAnnotationTransformer;

import org.testng.annotations.ITestAnnotation;

 

public class CalcAnnoTransformer implements IAnnotationTransformer {

       @Override

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

       {

           if ("concurrencyTest".equals(testMethod.getName())) {

             annotation.setThreadPoolSize(5);

           }

       }

}

 

To specify programmatically

TestNG tng = new TestNG();

// set listener AnnotationTransformer

tng.setAnnotationTransformer(new CalcAnnoTransformer());

 

To specify class on commandline

java org.testng.TestNG -listener CalcAnnoTransformertestng.xml