Make Selenium Easy

TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer

Hello Folks,

We have already learnt about IRetryAnalyzer interface in previous post. We know why we use it and how can we use it.

From previous post, you know that we can pass implemented class of IRetryAnalyzer as an value to attribute named “retryAnalyzer” at @Test annotation. You need to add it each and every @Test annotation method if you want retry mechanism for each test method. It might be difficult if you have a large number of Test methods. If you want to remove the logic, you may need to remove it from each @Test method. 

So there is an another way of implementing the same if you want to add retry mechanism for each and every Test method. That way is using Listeners named IAnnotationTransformer. IAnnotationTransformer is a powerful listener provided by TestNG to give you a chance to modify a TestNG annotation read from your test classes. You can get or change the values you need by calling any of the getter/setters on the ITest interface.  This interface has only method named “transform”.

Method “transform has an parameter of type ITestAnnotation interface which has method to set (setRetryAnalyzer) and get (getRetryAnalyzer) RetryAnalyzer at run time. You need to pass your implemented class of IRetryAnalyzer interface as an argument to setRetryAnalyzer() method.

Step to add IRetryAnalyzer as a Listener:

  1. Create an implemented class of IRetryAnalyzer interface and override retry method as required.

2. Create an implemented class of IAnnotationTransformer interface and override transform method as :

3. Create a TestNG class with some @Test method.

4. Add listener class name in testng.xml –

5. Run testng.xml as TestNG suite.


You can notice the same behaviour as previous post. It is just another way of implementing retry mechanism.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.