Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IAnnotationTransformer: Class level @Test annotoation causes "transform()" method invocation multiple times. #2730

Open
David91 opened this issue Feb 16, 2022 · 7 comments

Comments

@David91
Copy link

David91 commented Feb 16, 2022

TestNG version - 7.5
Java version - 17.0.2
Build tool - Maven, surefire plugin version 3.0.0-M5
IDE -InteliJ

When @Test annotation is set at class level and IAnnotationTransformer interface is implemented, transform() method gets invocated multiple times. I saw the same issue had been reported before and got fixed, but it appears again.

Here is my testng.xml file content attached.

image

The Test Class

@Test
public class SomeTestClass {

    public void someTest() {
        System.out.println("someTest");
    }
}

IAnnotationTransformer implementation class

public class AnnotationTransformer implements IAnnotationTransformer {

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

        System.out.println(testClass);
    }
}

And here is the output:

image

@bj-9527
Copy link
Contributor

bj-9527 commented Feb 18, 2022

If using @Listeners({AnnotationTransformer.class}) instead of add listeners in xml file, the transform seems not work.
image

@krmahadevan
Copy link
Member

@bj-9527

using @listeners({AnnotationTransformer.class}) instead of add listeners in xml file, the transform seems not work.

I think that's working as designed. The documentation may have not been updated to reflect this. This is because the annotation transformer needs to run before any other annotations are processed and so it cannot be supported via @Listeners annotation (Atleast that's the rationale I remember it having as)

@bj-9527
Copy link
Contributor

bj-9527 commented Feb 18, 2022

OK, that's reasonable, thanks for clarification.
By the way, @David91 could you please share what's the expected behavior for this test

@David91
Copy link
Author

David91 commented Feb 18, 2022

Hi @bj-9527
The expected behavior is that transform() method should be invoked once for the class "SomeTestClass".

@krmahadevan
Copy link
Member

@David91 - Couple of questions

  1. Do you see the same behavior when @Test is used on top of methods than types ?
  2. IAnnotationTransformer by its behavior only mentions to the users that it can be used to alter annotations, but I don't know/remember it guaranteeing that its going to get called only once. While this expectation is valid from a user, what I want to understand is that how is this affecting your test/project ?

The implementation is such that the transformer gets called multiple times at multiple phases etc., So this could perhaps be a bit of a tricky issue to solve.

@David91
Copy link
Author

David91 commented Feb 18, 2022

Hi @krmahadevan

  1. For method level @Test annotation transform() method works as expected. It gets invoked once
  2. I don't say only once in any case, but in this case, there is only one @Test annotation on top of the class, so it should be invoked once for that class.
    Also here is the description of the transform() method from TestNG official doc and it clearly describes when the method should be invoked.
    image

@krmahadevan
Copy link
Member

@David91

For method level @test annotation transform() method works as expected. It gets invoked once

Ok in that case, a class that has only 1 @Test method should also reflect the same behaviour. If that's not what is being seen then this for sure would be a defect.

I don't say only once in any case, but in this case, there is only one @test annotation on top of the class, so it should be invoked once for that class.
Fair expectation. I believe that there's a defect lurking there somewhere which needs to be taken care of.

Also here is the description of the transform() method from TestNG official doc and it clearly describes when the method should be invoked.

Yes, that's correct. But it doesn't call out how many times would the transform be invoked, which is what I was alluding towards :)

@krmahadevan krmahadevan added this to the 7.6.0 milestone Feb 18, 2022
@krmahadevan krmahadevan modified the milestones: 7.6.0, 7.7.0 May 18, 2022
@krmahadevan krmahadevan removed this from the 7.7.0 milestone Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants