overridden transform method of IAnnotationTransformer interface is not called/executed when listener is implemented internally using the @listener annotation. While it is working fine when executed from TestNG.xml
Test Class
@Listeners(Annotation.AnnotationTransform.class)
public class factory {
@
Test(priority = 1, invocationCount = 3)
public void t1() {
System.out.println("Method is t1, parameter is " + "one");
AssertJUnit.assertTrue(true);
}
@
Test(priority = 2)
public void t2() {
System.out.println("Method is t2, parameter is " + "two");
}
@
Test(priority = 3)
public void t3() {
System.out.println("Method is t3");
}
}
Listener Class
public class AnnotationTransform implements IAnnotationTransformer, IRetryAnalyzer {
@Override
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
if (testMethod.getName().equals("t1")) {
System.out.println("set data provider for " + testMethod.getName());
annotation.setInvocationCount(10);
} else if (testMethod.getName().equals("t3")) {
System.out.println("set data provider for " + testMethod.getName());
} else if (testMethod.getName().equals("t2")) {
System.out.println("Disable " + testMethod.getName());
annotation.setEnabled(false);
}
}
}
Running the above code as 'Run as TestNG Test' is not invoking transform method while it is fine when 'Run as TestNG Suite' from below testNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false" >
<listeners>
<listener class-name="Annotation.AnnotationTransform" />
</listeners>
<test name="Test" preserve-order="true">
<classes>
<class name="Annotation.factory" />
</classes>
</test>
</suite>
You need to mention any listener class that implements IAnnotationTransformer in testng.xml as @listeners annotation does not pick it up.
From the TestNg documentation -
The @Listeners annotation can contain any class that extends org.testng.ITestNGListener except IAnnotationTransformer and IAnnotationTransformer2. The reason is that these listeners need to be known very early in the process so that TestNG can use them to rewrite your annotations, therefore you need to specify these listeners in your testng.xml file.
URL - http://testng.org/doc/documentation-main.html#testng-listeners