TestNG 中的 IInvokedMethodListener 和 IMethodInterceptor 有什么区别?

Whats the differences between IInvokedMethodListener and IMethodInterceptor in TestNG?

java中拦截器和监听器的基本区别是什么?如果我同时添加两者,哪个会先被调用?

1) IMethodInterceptor 允许用户修改要执行的方法(或测试)列表。 2) IInvokedMethodListener 允许用户执行某些操作before/after 一个方法已经被执行。清理或设置之类的东西。 3) 首先调用 IMethodInterceptor,这样如果用户希望修改要执行的方法列表,那么用户可以更改它并将其传递给 TestRunner。 请看下面的示例代码:

    package practise;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener2;
import org.testng.ITestContext;
import org.testng.ITestResult;

public class InvokedMethodListener implements IInvokedMethodListener2
{
    @Override
    public void afterInvocation(IInvokedMethod arg0, ITestResult arg1)
    {
        System.out.println(Thread.currentThread().getStackTrace()[0].getMethodName());
    }

    @Override
    public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1)
    {
        System.out.println(Thread.currentThread().getStackTrace()[0].getMethodName());
    }

    @Override
    public void afterInvocation(IInvokedMethod arg0, ITestResult arg1, ITestContext arg2)
    {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @Override
    public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1, ITestContext arg2)
    {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

}


    package practise;

import java.util.List;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;

public class MethodInterceptor implements IMethodInterceptor
{

    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> arg0, ITestContext arg1)
    {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
        return arg0;
    }

}

    package practise;

import org.testng.annotations.Listeners;

@Listeners(value={MethodInterceptor.class,InvokedMethodListener.class})
public class Test
{
    @org.testng.annotations.Test
    public void first()
    {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    @org.testng.annotations.Test
    public void second()
    {
        System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}