Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created February 25, 2013 10:25
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/5028960 to your computer and use it in GitHub Desktop.
Save krmahadevan/5028960 to your computer and use it in GitHub Desktop.
This is a sample that shows how to use "IMethodInterceptor" in TestNG to order your tests. In this example I am ordering the tests based on their method names.
package org.rationale.emotions;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(MethodInterceptorDemo.MyInterceptor.class)
public class MethodInterceptorDemo {
@Test
public void A() {
System.out.println("Hello A");
}
@Test
public void C() {
System.out.println("Hello C");
}
@Test
public void B() {
System.out.println("Hello B");
}
@Test
public void Aaa() {
System.out.println("Hello Aaa");
}
public static class MyInterceptor implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> arg0, ITestContext arg1) {
Collections.sort(arg0, new MyComparator());
return arg0;
}
}
public static class MyComparator implements Comparator<IMethodInstance> {
@Override
public int compare(IMethodInstance o1, IMethodInstance o2) {
return o1.getMethod().getMethodName().compareTo(o2.getMethod().getMethodName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment