Best way to add custom BEFORE/AFTER annotation

162 views
Skip to first unread message

Baubak

unread,
Sep 26, 2021, 6:20:18 AM9/26/21
to testng-users
Hi there,

My current project is an open source plugin for TestNG (will be sharing it when I am happy with it), which introduces a category of tests with specific behaviors in a normal test project.

I would like to create my own @BeforeMyEvent method that is invoked before my category of tests are run.

There are a lot of workarounds I know. My question is if there is a clean way of doing that?

Obviously I can integrate it in a Test listener and invoke the @before method by parsing the code. Or set my tests in a dedicated group, and invoke beforeGroup. But ideally I would like to have my own annotation. Is theree a way to get my method to be invoked by the testng framework (picked by JDK15AnnotationFinder)?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

unread,
Sep 28, 2021, 12:42:08 AM9/28/21
to testng-users
Have you considered adding your custom annotation which gets parsed in a TestNG IInvokedMethodListener and depending on the annotation being found, invokes your custom behavior ? This is a standard way in which you can build custom annotation driven behavior.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/5c500c67-0846-43e6-9af9-a8947d74a371n%40googlegroups.com.

Baubak

unread,
Sep 28, 2021, 3:40:01 AM9/28/21
to testng-users
Thanks for your answer Krishnan,

What are the added values in using InvokedMethodListener rather than invoking it in another listener such as ITestListener?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

unread,
Sep 28, 2021, 4:02:59 AM9/28/21
to testng-users
IInvokedMethodListener would help you intercept both configuration and test methods, whereas the same is not going to be possible when you use ITestListener.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Baubak

unread,
Sep 28, 2021, 8:13:34 AM9/28/21
to testng-users
Hi Again Krishnan,

By "adding your custom annotation which gets parsed in a TestNG IInvokedMethodListener", do you mean that I search for it in in the IInvokedMethodListener implementation, or that there is a smart way of having my annotated method to be considered as a configuration method? If there is I'd love an example of how it could work.

Best regards,

Baubak

On Tuesday, September 28, 2021 at 6:42:08 AM UTC+2 Krishnan Mahadevan wrote:

Krishnan Mahadevan

unread,
Sep 28, 2021, 8:37:10 AM9/28/21
to testng...@googlegroups.com
Please see here for an example of what I am suggesting.


In the above class I look for a custom annotation before deciding to do some browser management.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Baubak

unread,
Sep 28, 2021, 9:06:58 AM9/28/21
to testng-users
Thanks a lot,

So if I understand you correctly the listener you have written assumes that the annotation should be on the test.

so it expects something like
@Test
@Browser
public void test() {
}

So what I can do is something like;

@Test
@MyCustomAnnoation() {
//Do stuff
}

And then when I reach it I can do the stuff inside?

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

unread,
Sep 28, 2021, 12:22:49 PM9/28/21
to testng-users
Yes. That is correct. You just need to switch the annotation and then the annotation parsing logic in the listener.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Baubak

unread,
Oct 3, 2021, 10:55:17 AM10/3/21
to testng-users
Hi,

After trying the IInvokedMethodListener, I decided that using another implementation of the IAnnotationTransformer would be more suitable for my use case:

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

It allowed me to intercept the configuration methods. In my case I wanted to make sure that in some cases my configuration method should be ignored. It seemed easier to implement that here. I am not sure the IInvokedMethodListener would allow me to disable a method execution.

Again, thanks for your suggetions :)

Baubak
Reply all
Reply to author
Forward
0 new messages