Testng: IAnnotationTransformer doesn't run when specified as an @Listeners

Created on 17 Sep 2013  ·  7Comments  ·  Source: cbeust/testng

IAnnotationTransformer only works when specified in the suite xml file with . I would like it to also run the transformer when specified in the @Listener annotation

Example:

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class TransformerImpl implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        System.out.println("Transform Annotation");
    }
}
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ TransformerImpl.class })
public class AnnotationTest {

    @Test
    public void run() {
        System.out.println("run");
    }
}

Most helpful comment

From the documentation: (http://testng.org/doc/documentation-main.html)

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. 

All 7 comments

Why this issue was closed? The problem is alive (v. 6.8.21).

From the documentation: (http://testng.org/doc/documentation-main.html)

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. 

Looks like the issue is still alive

​It wasn't an issue to begin with. The Javadocs I believe explicitly calls
out that IAnnotationTransformer cannot be injected via a @Listeners
annotation.
​TestNG is working as designed in this case.​

Hello,
So we would be able to add the IAnnotation Transformer as listener only in the testng.xml file not in the @Listener annotation is it?

Changes note says @Listeners is supported for IAnnotationTransformer since 6.9.10 version. However it still didn't work for me using 6.9.10.

@ChenaLee - Not true to the best of my knowledge. I think the javadocs makes this explicit.

Was this page helpful?
0 / 5 - 0 ratings