Source:TestNG tests synchronization on outer object
    private String foo;
    public String getFoo(){...}
    public void setFoo(String foo){...}; 
Best Testng code snippet using org.testng.Interface IExecutionListener
Source:AbstractListenerChain.java  
1package com.nordstrom.automation.testng;2import java.lang.annotation.Annotation;3import java.lang.reflect.Constructor;4import java.lang.reflect.InvocationTargetException;5import java.lang.reflect.Method;6import java.util.ArrayList;7import java.util.Collections;8import java.util.HashSet;9import java.util.List;10import java.util.Objects;11import java.util.ServiceLoader;12import java.util.Set;13import org.testng.IAnnotationTransformer;14import org.testng.IClassListener;15import org.testng.IConfigurationListener;16import org.testng.IExecutionListener;17import org.testng.IInvokedMethod;18import org.testng.IInvokedMethodListener;19import org.testng.IMethodInstance;20import org.testng.IMethodInterceptor;21import org.testng.ISuite;22import org.testng.ISuiteListener;23import org.testng.ITestClass;24import org.testng.ITestContext;25import org.testng.ITestListener;26import org.testng.ITestNGListener;27import org.testng.ITestResult;28import org.testng.annotations.ITestAnnotation;29import com.google.common.base.Optional;30import com.google.common.collect.Lists;31/**32 * This TestNG listener enables the addition of other listeners at runtime and guarantees the order in which they're33 * invoked. This is similar in behavior to a JUnit rule chain.34 */35public abstract class AbstractListenerChain implements IAnnotationTransformer, IExecutionListener, ISuiteListener,36		IConfigurationListener, IInvokedMethodListener, ITestListener, IMethodInterceptor, IClassListener {37    38    private Set<Class<?>> markedClasses = Collections.synchronizedSet(new HashSet<Class<?>>());39    private Set<Class<? extends ITestNGListener>> listenerSet = 40            Collections.synchronizedSet(new HashSet<Class<? extends ITestNGListener>>());41    42    protected List<ITestNGListener> listeners;43    protected List<IAnnotationTransformer> annotationXformers;44    protected List<IExecutionListener> executionListeners;45    protected List<ISuiteListener> suiteListeners;46    protected List<IConfigurationListener> configListeners;47    protected List<IInvokedMethodListener> methodListeners;48    protected List<ITestListener> testListeners;49    protected List<IMethodInterceptor> methodInterceptors;50    protected List<IClassListener> classListeners;51    52    private static final String LISTENER_CHAIN = "ListenerChain";53    54    public AbstractListenerChain() {55        initialize();56        for (LinkedListener listener : ServiceLoader.load(LinkedListener.class)) {57            attachListener(null, listener);58        }59    }60    /**61     * [IAnnotationTransformer]62     * This method will be invoked by TestNG to give you a chance to modify a TestNG annotation read from your test63     * classes. You can change the values you need by calling any of the setters on the ITest interface. Note that64     * only one of the three parameters testClass, testCtor and testMethod will be non-null.65     * 66     * @param annotation The annotation that was read from your test class.67     * @param testClass If the annotation was found on a class, this parameter represents this class (null otherwise).68     * @param testCtor If the annotation was found on a constructor, this parameter represents this constructor (null69     *        otherwise).70     * @param testMethod If the annotation was found on a method, this parameter represents this method (null71     *        otherwise).72     */73    @Override74    @SuppressWarnings("rawtypes")75    public void transform(ITestAnnotation annotation, Class testClass, Constructor testCtor, Method testMethod) {76        attachListeners(testClass, testCtor, testMethod);77        78        synchronized(annotationXformers) {79            for (IAnnotationTransformer annotationXformer : annotationXformers) {80                annotationXformer.transform(annotation, testClass, testCtor, testMethod);81            }82        }83    }84    /**85     * [IExecutionListener]86     * Invoked before the TestNG run starts.87     */88    @Override89    public void onExecutionStart() {90        synchronized(executionListeners) {91            for (IExecutionListener executionListener : executionListeners) {92                executionListener.onExecutionStart();93            }94        }95    }96    /**97     * [IExecutionListener]98     * Invoked once all the suites have been run.99     */100    @Override101    public void onExecutionFinish() {102        synchronized(executionListeners) {103            for (IExecutionListener executionListener : executionListeners) {104                executionListener.onExecutionFinish();105            }106        }107    }108    109    /**110     * [ISuiteListener]111     * This method is invoked before the SuiteRunner starts.112     * 113     * @param suite current test suite114     */115    @Override116    public void onStart(ISuite suite) {117        suite.setAttribute(LISTENER_CHAIN, this);118        119        synchronized(suiteListeners) {120            for (ISuiteListener suiteListener : Lists.reverse(suiteListeners)) {121                suiteListener.onStart(suite);122            }123        }124    }125    /**126     * [ISuiteListener]127     * This method is invoked after the SuiteRunner has run all128     * the test suites.129     * 130     * @param suite current test suite131     */132    @Override133    public void onFinish(ISuite suite) {134        synchronized(suiteListeners) {135            for (ISuiteListener suiteListener : suiteListeners) {136                suiteListener.onFinish(suite);137            }138        }139    }140    /**141     * [IConfigurationListener]142     * Invoked whenever a configuration method succeeded.143     * 144     * @param itr test result object for the associated configuration method145     */146    @Override147    public void onConfigurationSuccess(ITestResult itr) {148        synchronized(configListeners) {149            for (IConfigurationListener configListener : configListeners) {150                configListener.onConfigurationSuccess(itr);151            }152        }153    }154    /**155     * [IConfigurationListener]156     * Invoked whenever a configuration method failed.157     * 158     * @param itr test result object for the associated configuration method159     */160    @Override161    public void onConfigurationFailure(ITestResult itr) {162        synchronized(configListeners) {163            for (IConfigurationListener configListener : configListeners) {164                configListener.onConfigurationFailure(itr);165            }166        }167    }168    /**169     * [IConfigurationListener]170     * Invoked whenever a configuration method was skipped.171     * 172     * @param itr test result object for the associated configuration method173     */174    @Override175    public void onConfigurationSkip(ITestResult itr) {176        synchronized(configListeners) {177            for (IConfigurationListener configListener : configListeners) {178                configListener.onConfigurationSkip(itr);179            }180        }181    }182    /**183     * [IInvokedMethodListener]184     * Invoked before each test or configuration method is invoked by TestNG185     * 186     * @param method TestNG representation of the method that's about to be invoked187     * @param testResult test result object for the method that's about to be invoked188     */189    @Override190    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {191        // NOTE: This method will never be called192    }193    /**194     * [IInvokedMethodListener]195     * Invoked after each test or configuration method is invoked by TestNG196     * 197     * @param method TestNG representation of the method that's just been invoked198     * @param testResult test result object for the method that's just been invoked199     */200    @Override201    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {202        // NOTE: This method will never be called203    }204    205    /**206     * [IInvokedMethodListener(2)]207     * Invoked before each test or configuration method is invoked by TestNG208     * 209     * @param method TestNG representation of the method that's about to be invoked210     * @param testResult test result object for the method that's about to be invoked211     * @param context test context212     */213    // @Override omitted to avoid interface conflict214    public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {215        synchronized(methodListeners) {216            for (IInvokedMethodListener methodListener : Lists.reverse(methodListeners)) {217                methodListener.beforeInvocation(method, testResult);218            }219        }220    }221    /**222     * [IInvokedMethodListener(2)]223     * Invoked after each test or configuration method is invoked by TestNG224     * 225     * @param method TestNG representation of the method that's just been invoked226     * @param testResult test result object for the method that's just been invoked227     * @param context text context228     */229    // @Override omitted to avoid interface conflict230    public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {231        synchronized(methodListeners) {232            for (IInvokedMethodListener methodListener : methodListeners) {233                methodListener.afterInvocation(method, testResult);234            }235        }236    }237    /**238     * [ITestListener]239     * Invoked each time before a test will be invoked.240     * The {@code ITestResult} is only partially filled with the references to241     * class, method, start millis and status.242     *243     * @param result the partially filled {@code ITestResult}244     * @see ITestResult#STARTED245     */246    @Override247    public void onTestStart(ITestResult result) {248        synchronized(testListeners) {249            for (ITestListener testListener : Lists.reverse(testListeners)) {250                testListener.onTestStart(result);251            }252        }253    }254    /**255     * [ITestListener]256     * Invoked each time a test succeeds.257     *258     * @param result {@code ITestResult} containing information about the run test259     * @see ITestResult#SUCCESS260     */261    @Override262    public void onTestSuccess(ITestResult result) {263        synchronized (testListeners) {264            for (ITestListener testListener : testListeners) {265                testListener.onTestSuccess(result);266            }267        }268    }269    /**270     * [ITestListener]271     * Invoked each time a test fails.272     *273     * @param result {@code ITestResult} containing information about the run test274     * @see ITestResult#FAILURE275     */276    @Override277    public void onTestFailure(ITestResult result) {278        synchronized (testListeners) {279            for (ITestListener testListener : testListeners) {280                testListener.onTestFailure(result);281            }282        }283    }284    /**285     * [ITestListener]286     * Invoked each time a test is skipped.287     *288     * @param result {@code ITestResult} containing information about the run test289     * @see ITestResult#SKIP290     */291    @Override292    public void onTestSkipped(ITestResult result) {293        synchronized (testListeners) {294            for (ITestListener testListener : testListeners) {295                testListener.onTestSkipped(result);296            }297        }298    }299    /**300     * [ITestListener]301     * Invoked each time a method fails but has been annotated with302     * successPercentage and this failure still keeps it within the303     * success percentage requested.304     *305     * @param result {@code ITestResult} containing information about the run test306     * @see ITestResult#SUCCESS_PERCENTAGE_FAILURE307     */308    @Override309    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {310        synchronized (testListeners) {311            for (ITestListener testListener : testListeners) {312                testListener.onTestFailedButWithinSuccessPercentage(result);313            }314        }315    }316    /**317     * [ITestListener]318     * Invoked after the test class is instantiated and before319     * any configuration method is called.320     * 321     * @param context context for the test run322     */323    @Override324    public void onStart(ITestContext context) {325        synchronized (testListeners) {326            for (ITestListener testListener : Lists.reverse(testListeners)) {327                testListener.onStart(context);328            }329        }330    }331    /**332     * [ITestListener]333     * Invoked after all the tests have run and all their334     * Configuration methods have been called.335     * 336     * @param context context for the test run337     */338    @Override339    public void onFinish(ITestContext context) {340        synchronized (testListeners) {341            for (ITestListener testListener : testListeners) {342                testListener.onFinish(context);343            }344        }345    }346    /**347     * [IMethodInterceptor]348     * Invoked to enable alteration of the list of test methods that TestNG is about to run.349     * 350     * @param methods list of test methods.351     * @param context test context.352     * @return the list of test methods to run.353     */354    @Override355    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {356        synchronized (methodInterceptors) {357            for (IMethodInterceptor interceptor : methodInterceptors) {358                methods = interceptor.intercept(methods, context);359            }360        }361        return methods;362    }363    /**364     * [IClassListener]365     * Invoked after the test class is instantiated and before366     * {@link org.testng.annotations.BeforeClass @BeforeClass} 367     * configuration methods are called.368     * 369     * @param testClass TestNG representation for the current test class370     */371    @Override372    public void onBeforeClass(ITestClass testClass) {373        synchronized (classListeners) {374            for (IClassListener classListener : Lists.reverse(classListeners)) {375                classListener.onBeforeClass(testClass);376            }377        }378    }379    /**380     * [IClassListener]381     * Invoked after all of the test methods of the test class have been invoked382     * and before {@link org.testng.annotations.AfterClass @AfterClass}383     * configuration methods are called.384     * 385     * @param testClass TestNG representation for the current test class386     */387    @Override388    public void onAfterClass(ITestClass testClass) {389        synchronized (classListeners) {390            for (IClassListener classListener : classListeners) {391                classListener.onAfterClass(testClass);392            }393        }394    }395    396    /**397     * Get reference to an instance of the specified listener type.398     * 399     * @param <T> listener type400     * @param result TestNG test result object401     * @param listenerType listener type402     * @return optional listener instance403     */404    public static <T extends ITestNGListener> Optional<T>405            getAttachedListener(ITestResult result, Class<T> listenerType) {406        407        Objects.requireNonNull(result, "[result] must be non-null");408        return getAttachedListener(result.getTestContext(), listenerType);409    }410    411    /**412     * Get reference to an instance of the specified listener type.413     * 414     * @param <T> listener type415     * @param context TestNG test context object416     * @param listenerType listener type417     * @return optional listener instance418     */419    public static <T extends ITestNGListener> Optional<T>420            getAttachedListener(ITestContext context, Class<T> listenerType) {421        422        Objects.requireNonNull(context, "[context] must be non-null");423        return getAttachedListener(context.getSuite(), listenerType);424    }425    426    /**427     * Get reference to an instance of the specified listener type.428     * 429     * @param <T> listener type430     * @param suite TestNG suite object431     * @param listenerType listener type432     * @return optional listener instance433     */434    public static <T extends ITestNGListener> Optional<T>435            getAttachedListener(ISuite suite, Class<T> listenerType) {436        437        Objects.requireNonNull(suite, "[suite] must be non-null");438        Objects.requireNonNull(listenerType, "[listenerType] must be non-null");439        ListenerChain chain = (ListenerChain) suite.getAttribute(LISTENER_CHAIN);440        Objects.requireNonNull(chain, "Specified suite has no ListenerChain");441        return chain.getAttachedListener(listenerType);442    }443    /**444     * Get reference to an instance of the specified listener type.445     * 446     * @param <T> listener type447     * @param listenerType listener type448     * @return optional listener instance449     */450    @SuppressWarnings("unchecked")451    public <T extends ITestNGListener> Optional<T> getAttachedListener(Class<T> listenerType) {452        for (ITestNGListener listener : listeners) {453            if (listener.getClass() == listenerType) {454                return Optional.of((T) listener);455            }456        }457        return Optional.absent();458    }459    /**460     * Attach linked listeners that are active on the test class that contains the specified test method.461     * 462     * @param testMethod test method463     */464    protected void attachListeners(Method testMethod) {465        if (testMethod != null) {466            processLinkedListeners(testMethod.getDeclaringClass());467        }468    }469    470    /**471     * Attach linked listeners that are active on the test class defined by the specified test context. Note that only472     * one of the three parameters testClass, testCtor and testMethod will be non-null.473     * 474     * @param testClass If the annotation was found on a class, this parameter represents this class (null otherwise).475     * @param testCtor If the annotation was found on a constructor, this parameter represents this constructor (null476     *        otherwise).477     * @param testMethod If the annotation was found on a method, this parameter represents this method (null478     *        otherwise).479     */480    protected void attachListeners(Class<?> testClass, Constructor<?> testCtor, Method testMethod) {481        if (testClass != null) {482            processLinkedListeners(testClass);483        } else if (testCtor != null) {484            processLinkedListeners(testCtor.getDeclaringClass());485        } else if (testMethod != null) {486            processLinkedListeners(testMethod.getDeclaringClass());487        }488    }489    490    /**491     * Attach linked listeners that are active on the specified test class.492     * 493     * @param testClass test class494     */495    protected void attachListeners(Class<?> testClass) {496        if (testClass != null) {497            processLinkedListeners(testClass);498        }499    }500    501    /**502     * Process the {@link LinkedListeners} annotation of the specified test class.503     * 504     * @param testClass test class505     */506    protected void processLinkedListeners(Class<?> testClass) {507        Objects.requireNonNull(testClass, "[testClass] must be non-null");508        509        LinkedListeners annotation = testClass.getAnnotation(LinkedListeners.class);510        if (null != annotation) {511            Class<?> markedClass = getMarkedClass(testClass);512            if ( ! markedClasses.contains(markedClass)) {513                markedClasses.add(markedClass);514                for (Class<? extends ITestNGListener> listener : annotation.value()) {515                    attachListener(listener, null);516                }517            }518        }519    }520    521    protected void initialize() {522        listeners = new ArrayList<>();523        annotationXformers = new ArrayList<>();524        executionListeners = new ArrayList<>();525        suiteListeners = new ArrayList<>();526        configListeners = new ArrayList<>();527        methodListeners = new ArrayList<>();528        testListeners = new ArrayList<>();529        methodInterceptors = new ArrayList<>();530        classListeners = new ArrayList<>();531    }532    533    /**534     * Wrap the current listener chain with an instance of the specified listener class.535     * <p>536     * <b>NOTE</b>: The order in which listener methods are invoked is determined by the537     * order in which listener objects are added to the chain. Listener <i>before</i> methods538     * are invoked in last-added-first-called order. Listener <i>after</i> methods are invoked539     * in first-added-first-called order.<br>540     * <b>NOTE</b>: Only one instance of any given listener class will be included in the chain.541     * 542     * @param listenerTyp listener class to add to the chain (may be 'null')543     * @param listenerObj listener object to add to the chain (may be 'null')544     */545    protected void attachListener(Class<? extends ITestNGListener> listenerTyp, ITestNGListener listenerObj) { //NOSONAR546        Class<? extends ITestNGListener> type;547        ITestNGListener object;548        549        if ((listenerTyp == null) && (listenerObj == null)) {550            throw new IllegalArgumentException("Neither [listenerTyp] nor [listenerObj] was specified");551        } else if (listenerObj != null) {552            object = listenerObj;553            type = listenerObj.getClass();554        } else {555            object = null;556            type = listenerTyp;557        }558        559        if ( ! listenerSet.contains(type)) { //NOSONAR560            listenerSet.add(type);561            562            if (object == null) {563                try {564                    object = type.getDeclaredConstructor().newInstance();565                } catch (InstantiationException | IllegalAccessException | IllegalArgumentException566                        | InvocationTargetException | NoSuchMethodException | SecurityException e) {567                    throw new RuntimeException("Unable to instantiate listener: " + type.getName(), e);568                }569            }570            571            synchronized(listeners) {572                listeners.add(object);573            }574            575            bucketizeListener(object);576        }577    }578    protected void bucketizeListener(ITestNGListener object) {579        if (object instanceof IExecutionListener) {580            synchronized(executionListeners) {581                executionListeners.add((IExecutionListener) object);582            }583        }584        585        if (object instanceof ISuiteListener) {586            synchronized(suiteListeners) {587                suiteListeners.add((ISuiteListener) object);588            }589        }590        591        if (object instanceof ITestListener) {592            synchronized(testListeners) {593                testListeners.add((ITestListener) object);594            }595        }596        597        if (object instanceof IMethodInterceptor) {598            synchronized(methodInterceptors) {599                methodInterceptors.add((IMethodInterceptor) object);600            }601        }602        603        if (object instanceof IClassListener) {604            synchronized(classListeners) {605                classListeners.add((IClassListener) object);606            }607        }608    }609    610    /**611     * Get the class in the hierarchy of the specified test class that declares the {@link LinkedListeners} annotation.612     * 613     * @param testClass test class to be evaluated614     * @return class that declares the {@link LinkedListeners} annotation; {@code null} if annotation isn't found615     */616    private static Class<?> getMarkedClass(Class<?> testClass) {617        for (Class<?> thisClass = testClass; thisClass != null; thisClass = thisClass.getSuperclass()) {618            for (Annotation annotation : thisClass.getDeclaredAnnotations()) {619                if (annotation.annotationType().isAssignableFrom(LinkedListeners.class)) {620                    return thisClass;621                }622            }623        }624        return null;625    }626}...Source:Constants.java  
1package test.listeners.ordering;2public interface Constants {3  String IALTERSUITELISTENER_ALTER = "org.testng.IAlterSuiteListener.alter(List<XmlSuite> suites)";4  String IANNOTATIONTRANSFORMER_TRANSFORM_3_ARGS = "org.testng.IAnnotationTransformer.transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod)";5  String IANNOTATIONTRANSFORMER_TRANSFORM_4_ARGS = "org.testng.IAnnotationTransformer.transform(IConfigurationAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod)";6  String IANNOTATIONTRANSFORMER_DATAPROVIDER = "org.testng.IAnnotationTransformer.transform(IDataProviderAnnotation annotation, Method method)";7  String IANNOTATIONTRANSFORMER_FACTORY = "org.testng.IAnnotationTransformer.transform(IFactoryAnnotation annotation, Method method)";8  String METHODINTERCEPTOR_INTERCEPT = "org.testng.IMethodInterceptor.intercept(List<IMethodInstance> methods, ITestContext context)";9  String IEXECUTION_VISUALISER_CONSUME_DOT_DEFINITION = "org.testng.IExecutionVisualiser.consumeDotDefinition(String dotDefinition)";10  String IREPORTER_GENERATE_REPORT = "org.testng.IReporter.generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory)";11  String ISUITELISTENER_ON_START = "org.testng.ISuiteListener.onStart()";12  String ISUITELISTENER_ON_FINISH = "org.testng.ISuiteListener.onFinish()";13  String ITESTLISTENER_ON_START_TEST_METHOD = "org.testng.ITestListener.onTestStart(ITestResult result)";14  String ITESTLISTENER_ON_TEST_FAILURE_TEST_METHOD = "org.testng.ITestListener.onTestFailure(ITestResult result)";15  String ITESTLISTENER_ON_TEST_TIMEOUT_TEST_METHOD = "org.testng.ITestListener.onTestFailedWithTimeout(ITestResult result)";16  String ITESTLISTENER_ON_TEST_SUCCESS_TEST_METHOD = "org.testng.ITestListener.onTestSuccess(ITestResult result)";17  String ITESTLISTENER_ON_TEST_SKIPPED_TEST_METHOD = "org.testng.ITestListener.onTestSkipped(ITestResult result)";18  String ITESTLISTENER_ON_START_TEST_TAG = "org.testng.ITestListener.onStart(ITestContext context)";19  String ITESTLISTENER_ON_FINISH_TEST_TAG = "org.testng.ITestListener.onFinish(ITestContext context)";20  String ICLASSLISTENER_ON_BEFORE_CLASS = "org.testng.IClassListener.onBeforeClass(ITestClass testClass)";21  String ICLASSLISTENER_ON_AFTER_CLASS = "org.testng.IClassListener.onAfterClass(ITestClass testClass)";22  String IINVOKEDMETHODLISTENER_BEFORE_INVOCATION = "org.testng.IInvokedMethodListener.beforeInvocation(IInvokedMethod method, ITestResult testResult)";23  String IINVOKEDMETHODLISTENER_BEFORE_INVOCATION_WITH_CONTEXT = "org.testng.IInvokedMethodListener.beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context)";24  String IINVOKEDMETHODLISTENER_AFTER_INVOCATION = "org.testng.IInvokedMethodListener.afterInvocation(IInvokedMethod method, ITestResult testResult)";25  String IINVOKEDMETHODLISTENER_AFTER_INVOCATION_WITH_CONTEXT = "org.testng.IInvokedMethodListener.afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context)";26  String IEXECUTIONLISTENER_ON_EXECUTION_START = "org.testng.IExecutionListener.onExecutionStart()";27  String IEXECUTIONLISTENER_ON_EXECUTION_FINISH = "org.testng.IExecutionListener.onExecutionFinish()";28  String IDATAPROVIDERLISTENER_BEFORE_DATA_PROVIDER_EXECUTION = "org.testng.IDataProviderListener.beforeDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext)";29  String IDATAPROVIDERLISTENER_AFTER_DATA_PROVIDER_EXECUTION = "org.testng.IDataProviderListener.afterDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext)";30  String ICONFIGURATIONLISTENER_BEFORE_CONFIGURATION = "org.testng.IConfigurationListener.beforeConfiguration(ITestResult tr)";31  String ICONFIGURATIONLISTENER_ON_CONFIGURATION_SUCCESS = "org.testng.IConfigurationListener.onConfigurationSuccess(ITestResult itr)";32  String ICONFIGURATIONLISTENER_ON_CONFIGURATION_FAILURE = "org.testng.IConfigurationListener.onConfigurationFailure(ITestResult itr)";33  String ICONFIGURATIONLISTENER_ON_CONFIGURATION_SKIP = "org.testng.IConfigurationListener.onConfigurationSkip(ITestResult itr)";34}...Source:IConfiguration.java  
1package org.testng.internal;2import org.testng.IConfigurable;3import org.testng.IConfigurationListener;4import org.testng.IExecutionListener;5import org.testng.IHookable;6import org.testng.ITestObjectFactory;7import org.testng.internal.annotations.IAnnotationFinder;8import java.util.List;9public interface IConfiguration {10  IAnnotationFinder getAnnotationFinder();11  void setAnnotationFinder(IAnnotationFinder finder);12  ITestObjectFactory getObjectFactory();13  void setObjectFactory(ITestObjectFactory m_objectFactory);14  IHookable getHookable();15  void setHookable(IHookable h);16  IConfigurable getConfigurable();17  void setConfigurable(IConfigurable c);18  List<IExecutionListener> getExecutionListeners();19  void addExecutionListener(IExecutionListener l);20  List<IConfigurationListener> getConfigurationListeners();21  void addConfigurationListener(IConfigurationListener cl);22}...Source:MyTestNGListeners.java  
1package listeners;2import org.testng.IAnnotationTransformer;3import org.testng.IExecutionListener;4import org.testng.IHookable;5import org.testng.IInvokedMethodListener;6import org.testng.IMethodInterceptor;7import org.testng.IReporter;8import org.testng.ISuiteListener;9import org.testng.ITestListener;10public interface MyTestNGListeners extends IAnnotationTransformer, 11											IHookable, 12											IExecutionListener, 13											IInvokedMethodListener,14											IMethodInterceptor,15											IReporter,16											ISuiteListener,17											ITestListener18{19}...Source:ITestListeners.java  
1package jp.co.rakuten.travel.framework.listeners;2import org.testng.IExecutionListener;3import org.testng.ISuiteListener;4import org.testng.ITestListener;5public interface ITestListeners extends IExecutionListener, ISuiteListener, ITestListener6{7    final String NOT_YET_IMPLEMENTED = "NOT yet implemented";8}...Interface IExecutionListener
Using AI Code Generation
1import org.testng.IExecutionListener;2public class ExecutionListener implements IExecutionListener {3public void onExecutionStart() {4System.out.println("TestNG is going to start");5}6public void onExecutionFinish() {7System.out.println("TestNG has finished, all tests have run");8}9}10import org.testng.ISuite;11import org.testng.ISuiteListener;12public class SuiteListener implements ISuiteListener {13public void onStart(ISuite arg0) {14System.out.println("About to begin executing Suite " + arg0.getName());15}16public void onFinish(ISuite arg0) {17System.out.println("About to end executing Suite " + arg0.getName());18}19}20import org.testng.ITestContext;21import org.testng.ITestListener;22import org.testng.ITestResult;23public class TestListener implements ITestListener {24public void onTestStart(ITestResult result) {25System.out.println("The execution of the main test starts now");26}27public void onTestSuccess(ITestResult result) {28System.out.println("The main test ends with success");29}30public void onTestFailure(ITestResult result) {31System.out.println("The main test ends with failure");32}33public void onTestSkipped(ITestResult result) {34System.out.println("The main test was skipped");35}36public void onTestFailedButWithinSuccessPercentage(ITestResult result) {37}38public void onStart(ITestContext context) {39System.out.println("About to begin executing Test " + context.getName());40}41public void onFinish(ITestContext context) {42System.out.println("Completed executing test " + context.getName());43}44}45import org.testng.IClassListener;46import org.testng.ITestClass;47public class ClassListener implements IClassListener {48public void onBeforeClass(ITestClass testClass, ITestContext context) {49System.out.println("About to begin executing Class " + testClass.getName());50}51public void onAfterClass(ITestClass testClass, ITestContext context) {52System.out.println("Completed executing test " + testClass.getName());53}54}55import org.testng.IInvokedMethod;56import org.testng.IInvokedMethodListener;57import org.testng.ITestResult;Interface IExecutionListener
Using AI Code Generation
1package com.test;2import org.testng.IExecutionListener;3public class ExecutionListener implements IExecutionListener {4    public void onExecutionStart() {5        System.out.println("onExecutionStart");6    }7    public void onExecutionFinish() {8        System.out.println("onExecutionFinish");9    }10}11package com.test;12import org.testng.ITestListener;13import org.testng.ITestResult;14public class TestListener implements ITestListener {15    public void onTestStart(ITestResult result) {16        System.out.println("onTestStart");17    }18    public void onTestSuccess(ITestResult result) {19        System.out.println("onTestSuccess");20    }21    public void onTestFailure(ITestResult result) {22        System.out.println("onTestFailure");23    }24    public void onTestSkipped(ITestResult result) {25        System.out.println("onTestSkipped");26    }27    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {28        System.out.println("onTestFailedButWithinSuccessPercentage");29    }30    public void onStart(ITestContext context) {31        System.out.println("onStart");32    }33    public void onFinish(ITestContext context) {34        System.out.println("onFinish");35    }36}37package com.test;38import org.testng.IReporter;39import org.testng.ISuite;40import org.testng.xml.XmlSuite;41public class Reporter implements IReporter {42    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {43        System.out.println("generateReport");44    }45}46package com.test;47import org.testng.IInvokedMethod;48import org.testng.IInvokedMethodListener;49import org.testng.ITestResult;50public class InvokedMethodListener implements IInvokedMethodListener {51    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {52        System.out.println("beforeInvocation");53    }54    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {Interface IExecutionListener
Using AI Code Generation
1package com.test;2import org.testng.IExecutionListener;3public class ExecutionListener implements IExecutionListener {4    public void onExecutionStart() {5        System.out.println("Execution Started");6    }7    public void onExecutionFinish() {8        System.out.println("Execution Finished");9    }10}11package com.test;12import org.testng.IInvokedMethod;13import org.testng.IInvokedMethodListener;14import org.testng.ITestResult;15public class InvokedMethodListener implements IInvokedMethodListener {16    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {17        System.out.println("Before Invocation of method");18    }19    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {20        System.out.println("After Invocation of method");21    }22}23package com.test;24import org.testng.ITestContext;25import org.testng.ITestListener;26import org.testng.ITestResult;27public class TestListener implements ITestListener {28    public void onTestStart(ITestResult result) {29        System.out.println("Test Started");30    }31    public void onTestSuccess(ITestResult result) {32        System.out.println("Test Success");33    }34    public void onTestFailure(ITestResult result) {35        System.out.println("Test Failed");36    }37    public void onTestSkipped(ITestResult result) {38        System.out.println("Test Skipped");39    }40    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {41        System.out.println("Test Failed but within success percentage");42    }43    public void onStart(ITestContext context) {44        System.out.println("Test Started");45    }46    public void onFinish(ITestContext context) {47        System.out.println("Test Finished");48    }49}50package com.test;51import org.testng.IReporter;52import org.testng.ISuite;53import org.testng.xml.XmlSuite;54import java.util.List;55public class Reporter implements IReporter {56    public void generateReport(List<XmlSuite> xmlSuites, List<ISuiteInterface IExecutionListener
Using AI Code Generation
1package com.test;2import org.testng.IExecutionListener;3public class ExecutionListener implements IExecutionListener {4    public void onExecutionStart() {5        System.out.println("Execution Started");6    }7    public void onExecutionFinish() {8        System.out.println("Execution Finished");9    }10}11package com.test;12import org.testng.ITestListener;13import org.testng.ITestResult;14public class TestListener implements ITestListener {15    public void onTestStart(ITestResult result) {16        System.out.println("Test Started");17    }18    public void onTestSuccess(ITestResult result) {19        System.out.println("Test Success");20    }21    public void onTestFailure(ITestResult result) {22        System.out.println("Test Failed");23    }24    public void onTestSkipped(ITestResult result) {25        System.out.println("Test Skipped");26    }27    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {28        System.out.println("Test Failed But Within Success Percentage");29    }30    public void onStart(ITestResult result) {31        System.out.println("Test Started");32    }33    public void onFinish(ITestResult result) {34        System.out.println("Test Finished");35    }36}37package com.test;38import org.testng.IInvokedMethod;39import org.testng.IInvokedMethodListener;40import org.testng.ITestResult;41public class InvokedMethodListener implements IInvokedMethodListener {42    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {43        System.out.println("Before Invocation");44    }45    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {46        System.out.println("After Invocation");47    }48}49package com.test;50import org.testng.IAnnotationTransformer;51import org.testng.annotations.ITestAnnotation;52import java.lang.reflect.Constructor;53import java.lang.reflect.Method;54public class AnnotationTransformer implements IAnnotationTransformer {55    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {Interface IExecutionListener
Using AI Code Generation
1public class TestNGListener implements IExecutionListener {2	public void onExecutionStart() {3		System.out.println("TestNG is going to start");4	}5	public void onExecutionFinish() {6		System.out.println("TestNG has finished, TestNGListener report");7	}8}9public class TestNGListener implements ITestListener {10	public void onTestStart(ITestResult result) {11		System.out.println("Test started: " + result.getName());12	}13	public void onTestSuccess(ITestResult result) {14		System.out.println("Test succeed: " + result.getName());15	}16	public void onTestFailure(ITestResult result) {17		System.out.println("Test failed: " + result.getName());18	}19	public void onTestSkipped(ITestResult result) {20		System.out.println("Test skipped: " + result.getName());21	}22	public void onTestFailedButWithinSuccessPercentage(ITestResult result) {23		System.out.println("Test failed but within success percentage: " + result.getName());24	}25	public void onStart(ITestContext context) {26		System.out.println("Test started: " + context.getName());27	}28	public void onFinish(ITestContext context) {29		System.out.println("Test finished: " + context.getName());30	}31}32public class TestNGListener implements ISuiteListener {33	public void onStart(ISuite suite) {34		System.out.println("Suite started: " + suite.getName());35	}36	public void onFinish(ISuite suite) {37		System.out.println("Suite finished: " + suite.getName());38	}39}40public class TestNGListener implements IInvokedMethodListener {41	public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {42		System.out.println("Test started: " + testResult.getName());43	}44	public void afterInvocation(IInvokedMethod method, ITestResult testResult) {45		System.out.println("Test finished: " + testResult.getName());46	}47}Interface IExecutionListener
Using AI Code Generation
1package com.test;2import java.io.File;3import java.io.FileWriter;4import java.io.IOException;5import java.text.SimpleDateFormat;6import java.util.Date;7import java.util.List;8import org.testng.IExecutionListener;9import org.testng.IReporter;10import org.testng.ISuite;11import org.testng.ITestContext;12import org.testng.ITestListener;13import org.testng.ITestResult;14import org.testng.TestListenerAdapter;15import org.testng.annotations.AfterClass;16import org.testng.annotations.BeforeClass;17import org.testng.annotations.Test;18import org.testng.xml.XmlSuite;19public class TestNGListener extends TestListenerAdapter implements ITestListener, IReporter, IExecutionListener {20    private static final String TESTNG_OUTPUT_FILE = "testng-results.xml";21    private static final String TESTNG_REPORT_FILE = "testng-report.html";22    private static final String TESTNG_REPORT_HEADER = "testng-report-header.html";23    private static final String TESTNG_REPORT_FOOTER = "testng-report-footer.html";24    private static final String TESTNG_REPORT_CSS = "testng-report.css";25";26";27";28    private static final String TESTNG_REPORT_HEADER_CSS = "body {font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #000000;}\r29";30    private static final String TESTNG_REPORT_HEADER_CSS2 = "table {font-family: Arial, Helvetica, sans-serif; fontInterface IExecutionListener
Using AI Code Generation
1package com.test;2import org.testng.IExecutionListener;3public class MyExecutionListener implements IExecutionListener {4    public void onExecutionStart() {5        System.out.println("Execution started");6    }7    public void onExecutionFinish() {8        System.out.println("Execution finished");9    }10}11package com.test;12import org.testng.ITestListener;13import org.testng.ITestResult;14public class MyTestListener implements ITestListener {15    public void onTestStart(ITestResult result) {16        System.out.println("Test started");17    }18    public void onTestSuccess(ITestResult result) {19        System.out.println("Test successful");20    }21    public void onTestFailure(ITestResult result) {22        System.out.println("Test failed");23    }24    public void onTestSkipped(ITestResult result) {25        System.out.println("Test skipped");26    }27    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {28        System.out.println("Test failed but within success percentage");29    }30    public void onStart(ITestContext context) {31        System.out.println("Test started");32    }33    public void onFinish(ITestContext context) {34        System.out.println("Test finished");35    }36}37package com.test;38import org.testng.ISuiteListener;39import org.testng.ISuite;40public class MySuiteListener implements ISuiteListener {41    public void onStart(ISuite suite) {42        System.out.println("Suite started");43    }44    public void onFinish(ISuite suite) {45        System.out.println("Suite finished");46    }47}48package com.test;49import org.testng.IClassListener;50import org.testng.ITestClass;51public class MyClassListener implements IClassListener {52    public void onBeforeClass(ITestClass testClass) {53        System.out.println("Before class");54    }55    public void onAfterClass(ITestClass testClass) {56        System.out.println("After class");57    }58}Interface IExecutionListener
Using AI Code Generation
1package testNG;2import org.testng.IExecutionListener;3{4    public void onExecutionStart() {5        System.out.println("TestNG is going to start");6    }7    public void onExecutionFinish() {8        System.out.println("TestNG has finished, all tests have run");9    }10}11package testNG;12import org.testng.ITestContext;13import org.testng.ITestListener;14import org.testng.ITestResult;15{16    public void onTestStart(ITestResult result) {17        System.out.println("Test started: " + result.getName());18    }19    public void onTestSuccess(ITestResult result) {20        System.out.println("Test success: " + result.getName());21    }22    public void onTestFailure(ITestResult result) {23        System.out.println("Test failed: " + result.getName());24    }25    public void onTestSkipped(ITestResult result) {26        System.out.println("Test skipped: " + result.getName());27    }28    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {29    }30    public void onStart(ITestContext context) {31    }32    public void onFinish(ITestContext context) {33    }34}35package testNG;36import org.testng.ISuite;37import org.testng.ISuiteListener;38{39    public void onStart(ISuite suite) {40        System.out.println("Suite started: " + suite.getName());41    }42    public void onFinish(ISuite suite) {43        System.out.println("Suite finished: " + suite.getName());44    }45}46package testNG;47import org.testng.IInvokedMethod;48import org.testng.IInvokedMethodListener;49import org.testng.ITestResult;50{51    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {52        System.out.println("Before invocation of method: " + method.get1    private String foo;2    public String getFoo(){...}3    public void setFoo(String foo){...}; 4TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
