1 package test.methodinterceptors; 2 3 import org.testng.IMethodInstance; 4 import org.testng.IMethodInterceptor; 5 import org.testng.ITestContext; 6 7 import java.util.ArrayList; 8 import java.util.List; 9 10 /** 11 * An interceptor that removes the method called "two". 12 */ 13 public class RemoveAMethodInterceptor implements IMethodInterceptor { 14 15 @Override intercept(List<IMethodInstance> methods, ITestContext context)16 public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { 17 List<IMethodInstance> result = new ArrayList<>(); 18 19 for (IMethodInstance method : methods) { 20 String name = method.getMethod().getMethodName(); 21 if (!name.equals("two")) { 22 result.add(method); 23 } 24 } 25 26 log(this, methods, result); 27 28 return result; 29 } 30 log(IMethodInterceptor listener, List<IMethodInstance> input, List<IMethodInstance> output)31 public static void log(IMethodInterceptor listener, List<IMethodInstance> input, 32 List<IMethodInstance> output) { 33 StringBuilder msg = new StringBuilder().append(listener.getClass().getName()) 34 .append(" - Input:").append(getMethodNames(input)).append(" ").append(input.size()) 35 .append(" methods.").append(" Output:").append(getMethodNames(output)).append(" ") 36 .append(output.size()).append(" methods"); 37 log(msg.toString()); 38 } 39 getMethodNames(List<IMethodInstance> methods)40 public static List<String> getMethodNames(List<IMethodInstance> methods) { 41 List<String> names = new ArrayList<>(); 42 for (IMethodInstance m : methods) { 43 names.add(m.getMethod().getMethodName()); 44 } 45 return names; 46 } 47 log(String s)48 private static void log(String s) { 49 // System.out.println("[MI2] " + s); 50 } 51 }