Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created August 19, 2011 02:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krmahadevan/1155871 to your computer and use it in GitHub Desktop.
Save krmahadevan/1155871 to your computer and use it in GitHub Desktop.
This class demonstrates on how to use AnnotationTransformer as a listener and inject group dependency on runtime. Here the problem statement is there are two classes AmDependent and AmIndependent. These two classes should be run sequentially but the test
import org.testng.annotations.Test;
@Test
public class AmDependent {
public void methodD() {
System.out.println(this.getClass().getSimpleName()
+ ".method D : Thread ID : " + Thread.currentThread().getId());
}
public void methodE() {
System.out.println(this.getClass().getSimpleName()
+ ".method E : Thread ID : " + Thread.currentThread().getId());
}
public void methodF() {
System.out.println(this.getClass().getSimpleName()
+ ".method F : Thread ID : " + Thread.currentThread().getId());
}
}
import org.testng.annotations.Test;
@Test
public class AmIndependent {
public void methodA() {
System.out.println(this.getClass().getSimpleName()
+ ".method A : Thread ID :" + Thread.currentThread().getId());
}
public void methodB() {
System.out.println(this.getClass().getSimpleName()
+ ". method B : Thread ID : " + Thread.currentThread().getId());
}
public void methodC() {
System.out.println(this.getClass().getSimpleName()
+ ". method C : Thread ID : " + Thread.currentThread().getId());
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class GroupsDependencyRuntimeInjector implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
String []values = GroupTracker.getInstance().setDependency();
String []groups = {values[0]};
annotation.setGroups(groups);
String []dependsOnGroups = {values[1]};
dependsOnGroups[0] = values[1];
if (!values[1].isEmpty())
annotation.setDependsOnGroups(dependsOnGroups);
System.out.println("Generated groupName = " + values[0]);
System.out.println("Generated dependsOnGroupsName =" + values[1]);
}
}
public class GroupTracker {
private String groupName = null;
private String dependsOnGroups = null;
private String lastGroupName = null;
private boolean isMasterGroupTaken = false;
private static GroupTracker gp = null;
private GroupTracker(){
}
public static GroupTracker getInstance(){
if (gp == null){
gp = new GroupTracker();
gp.groupName = "MasterGroup";
gp.dependsOnGroups = "";
}
return gp;
}
public synchronized String[] setDependency(){
String[] returnValues = new String[2];
if (isMasterGroupTaken){
this.lastGroupName = groupName;
groupName = "Group" + System.nanoTime();
this.dependsOnGroups = this.lastGroupName;
}
if ("MasterGroup".equals(groupName)){
this.isMasterGroupTaken = true;
}
returnValues[0] = groupName;
returnValues[1] = dependsOnGroups;
return returnValues;
}
}
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<listeners>
<listener class-name="mypackage.GroupsDependencyRuntimeInjector" />
</listeners>
<test name="Test">
<packages>
<package name="mypackage" />
</packages>
</test>
</suite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment