02
Sep

IAnnotationTransformer to manipulate TestNG annotations, testclass, constructor and Methods at run-time

IAnnotationTransformer to manipulate TestNG annotations, testclass, constructor and Methods at run-time


IAnnotationTransformer to manipulate TestNG annotations, testclass, constructor and Methods at run-time
========================================================================================================

Step 1:  Create Custom implementation for IAnnotationTransformer
=================================================================

package testNG.IAnnotationTransformer.Examples;

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

public class My_ITestAnnotationTransformer implements IAnnotationTransformer {
	@Override
	public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
		System.out.println(testMethod.getName() + "$$$$$$$$$$$$");
		if (testMethod.getName().contains("myFunction")) {
			//add annotation enabled=false to @Test(xxxx) annotation
			annotation.setEnabled(false);
		}
		if (testMethod.getName().equals("test_Disabled")) {
			//add annotation enabled=true to @Test(xxxx) annotation
			annotation.setEnabled(true);
		}
	}
}


Step 2: Create your TestNG Methods:
====================================

package testNG.IAnnotationTransformer.Examples;

import org.testng.annotations.Test;

public class ActualTestNGMethods {

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


Step 3:  Associate your custom implementation to TestNG Methods via Testng.xml
===============================================================================

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<listeners>
		<listener 
			class-name="testNG.IAnnotationTransformer.Examples.My_ITestAnnotationTransformer"></listener>
	</listeners>
	<test thread-count="5" name="Test">
		<classes>
			<class name="testNG.IAnnotationTransformer.Examples.ActualTestNGMethods" />
		</classes>
	</test>
</suite> 


Output:
=======

myFunction$$$$$$$$$$$$
[TestNG] Running:
  C:\SRTemp\guice\guice\testng.xml


===============================================
Suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================



http://defectracker.com/wp-content/uploads/2019/09/IAnnotationTransformer7z.txt