Created
February 25, 2013 10:25
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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