Hi,
I've wrote IAnnotationTransformer2 listener to change configuration
annotation for some circumstances, and surplisingly found out that
IConfigurationAnnotation is not properly initialized before passing to
IAnnotationTransformer2 transform method. All field are still default
- falses and nulls.
So e.g. I expect AfterTest annotation, I use
IConfigurationAnnotation.getAfterTest() method to check, but it always
returns false. Others fields, the same. I've checked TestNg versions
from 6.0 to 6.3.
*Example*:
Test class:
package pl.filus;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
public class AnnotationDisableListenerTest {
@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
@Test
public void testing() {
System.out.println("Testing");
}
@AfterTest
public void afterTest() {
assert false;
}
}
Listener:
package pl.filus;
import org.testng.IAnnotationTransformer2;
import org.testng.annotations.IConfigurationAnnotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.annotations.IDataProviderAnnotation;
import org.testng.annotations.IFactoryAnnotation;
import org.testng.annotations.ITestAnnotation;
public class AnnotationDisableListener implements
IAnnotationTransformer2 {
public void transform(IConfigurationAnnotation
iConfigurationAnnotation, Class aClass, Constructor constructor,
Method method) {
System.out.format("%s %s %s\r\n", method.getName(),
iConfigurationAnnotation.getBeforeTest(),
iConfigurationAnnotation.getAfterTest());
iConfigurationAnnotation.setEnabled(false); //this works.
}
public void transform(IDataProviderAnnotation
iDataProviderAnnotation, Method method) {
//Default implementation
}
public void transform(IFactoryAnnotation iFactoryAnnotation, Method
method) {
//Default implementation
}
public void transform(ITestAnnotation iTestAnnotation, Class aClass,
Constructor constructor, Method method) {
//Default implementation
}
}
testng.xml:
<!DOCTYPE suite SYSTEM "
http://testng.org/testng-1.0.dtd" >
<suite name="Suite">
<listeners>
<listener class-name="pl.filus.AnnotationDisableListener"/>
</listeners>
<test name="Test">
<classes>
<class name="pl.filus.AnnotationDisableListenerTest"/>
</classes>
</test>
</suite>
In result we'll see on console:
beforeTest false false
afterTest false false
Testing
But it should be:
beforeTest true false
afterTest false true
Testing