Best Testng code snippet using org.testng.Interface IConfigurationListener2
Source:ListenerChain.java  
1package com.nordstrom.automation.testng;23import java.lang.reflect.Constructor;4import java.lang.reflect.Method;5import java.util.ArrayList;6import java.util.List;7import org.testng.IAnnotationTransformer;8import org.testng.IAnnotationTransformer2;9import org.testng.IAnnotationTransformer3;10import org.testng.IConfigurationListener;11import org.testng.IConfigurationListener2;12import org.testng.IInvokedMethod;13import org.testng.IInvokedMethodListener;14import org.testng.IInvokedMethodListener2;15import org.testng.ITestContext;16import org.testng.ITestNGListener;17import org.testng.ITestResult;18import org.testng.annotations.IConfigurationAnnotation;19import org.testng.annotations.IDataProviderAnnotation;20import org.testng.annotations.IFactoryAnnotation;21import org.testng.annotations.IListenersAnnotation;22import org.testng.annotations.ITestAnnotation;23import org.testng.internal.InvokedMethod;2425import com.google.common.collect.Lists;2627/**28 * This TestNG listener enables the addition of other listeners at runtime and guarantees the order in which they're29 * invoked. This is similar in behavior to a JUnit rule chain.30 */31public class ListenerChain extends AbstractListenerChain32                implements IAnnotationTransformer3, IConfigurationListener2, IInvokedMethodListener2 {33    34    private List<IAnnotationTransformer2> annotationXformers2;35    private List<IAnnotationTransformer3> annotationXformers3;36    private List<IConfigurationListener2> configListeners2;37    private List<IInvokedMethodListener2> methodListeners2;38    39    /**40     * [IAnnotationTransformer]41     * This method will be invoked by TestNG to give you a chance to modify a TestNG annotation read from your test42     * classes. You can change the values you need by calling any of the setters on the ITest interface. Note that43     * only one of the three parameters testClass, testCtor and testMethod will be non-null.44     * 45     * @param annotation The annotation that was read from your test class.46     * @param testClass If the annotation was found on a class, this parameter represents this class (null otherwise).47     * @param testCtor If the annotation was found on a constructor, this parameter represents this constructor (null48     *        otherwise).49     * @param testMethod If the annotation was found on a method, this parameter represents this method (null50     *        otherwise).51     */52    @Override53    @SuppressWarnings("rawtypes")54    public void transform(ITestAnnotation annotation, Class testClass, Constructor testCtor, Method testMethod) {55        attachListeners(testClass, testCtor, testMethod);56        57        super.transform(annotation, testClass, testCtor, testMethod);58        synchronized(annotationXformers2) {59            for (IAnnotationTransformer2 annotationXformer : annotationXformers2) {60                annotationXformer.transform(annotation, testClass, testCtor, testMethod);61            }62        }63        synchronized(annotationXformers3) {64            for (IAnnotationTransformer3 annotationXformer : annotationXformers3) {65                annotationXformer.transform(annotation, testClass, testCtor, testMethod);66            }67        }68    }6970    /**71     * [IAnnotationTransformer2]72     * Transform an IConfiguration annotation. Note that only one of the three parameters testClass, testCtor and73     * testMethod will be non-null.74     * 75     * @param annotation The annotation that was read from your test class.76     * @param testClass If the annotation was found on a class, this parameter represents this class (null otherwise).77     * @param testCtor If the annotation was found on a constructor, this parameter represents this constructor (null78     *        otherwise).79     * @param testMethod If the annotation was found on a method, this parameter represents this method (null80     *        otherwise).81     */82    @Override83    @SuppressWarnings("rawtypes")84    public void transform(IConfigurationAnnotation annotation, Class testClass,85                    Constructor testCtor, Method testMethod) {86        87        attachListeners(testClass, testCtor, testMethod);8889        synchronized(annotationXformers2) {90            for (IAnnotationTransformer2 annotationXformer : annotationXformers2) {91                annotationXformer.transform(annotation, testClass, testCtor, testMethod);92            }93        }94        synchronized(annotationXformers3) {95            for (IAnnotationTransformer3 annotationXformer : annotationXformers3) {96                annotationXformer.transform(annotation, testClass, testCtor, testMethod);97            }98        }99    }100101    /**102     * [IAnnotationTransformer2]103     * Transform an IDataProvider annotation.104     * 105     * @param annotation The data provider annotation.106     * @param method The method annotated with the IDataProvider annotation.107     */108    @Override109    public void transform(IDataProviderAnnotation annotation, Method method) {110        attachListeners(method);111        112        synchronized(annotationXformers2) {113            for (IAnnotationTransformer2 annotationXformer : annotationXformers2) {114                annotationXformer.transform(annotation, method);115            }116        }117        synchronized(annotationXformers3) {118            for (IAnnotationTransformer3 annotationXformer : annotationXformers3) {119                annotationXformer.transform(annotation, method);120            }121        }122    }123124    /**125     * [IAnnotationTransformer2]126     * Transform an IFactory annotation.127     * 128     * @param annotation The factory annotation.129     * @param method The method annotated with the IFactory annotation.130     */131    @Override132    public void transform(IFactoryAnnotation annotation, Method method) {133        attachListeners(method);134        135        synchronized(annotationXformers2) {136            for (IAnnotationTransformer2 annotationXformer : annotationXformers2) {137                annotationXformer.transform(annotation, method);138            }139        }140        synchronized(annotationXformers3) {141            for (IAnnotationTransformer3 annotationXformer : annotationXformers3) {142                annotationXformer.transform(annotation, method);143            }144        }145    }146147    /**148     * [IAnnotationTransformer3]149     * Transform a Listeners annotation.150     * 151     * @param annotation The listeners annotation.152     * @param testClass The test class annotated with the Listeners annotation.153     */154    @Override155    @SuppressWarnings("rawtypes")156    public void transform(IListenersAnnotation annotation, Class testClass) {157        attachListeners(testClass);158        159        synchronized(annotationXformers3) {160            for (IAnnotationTransformer3 annotationXformer : annotationXformers3) {161                annotationXformer.transform(annotation, testClass);162            }163        }164    }165166    /**167     * [IConfigurationListener]168     * Invoked whenever a configuration method succeeded.169     * 170     * @param itr test result object for the associated configuration method171     */172    @Override173    public void onConfigurationSuccess(ITestResult itr) {174        super.onConfigurationSuccess(itr);175        synchronized(configListeners2) {176            for (IConfigurationListener2 configListener : configListeners2) {177                configListener.onConfigurationSuccess(itr);178            }179        }180    }181182    /**183     * [IConfigurationListener]184     * Invoked whenever a configuration method failed.185     * 186     * @param itr test result object for the associated configuration method187     */188    @Override189    public void onConfigurationFailure(ITestResult itr) {190        super.onConfigurationFailure(itr);191        synchronized(configListeners2) {192            for (IConfigurationListener2 configListener : configListeners2) {193                configListener.onConfigurationFailure(itr);194            }195        }196    }197198    /**199     * [IConfigurationListener]200     * Invoked whenever a configuration method was skipped.201     * 202     * @param itr test result object for the associated configuration method203     */204    @Override205    public void onConfigurationSkip(ITestResult itr) {206        super.onConfigurationSkip(itr);207        synchronized(configListeners2) {208            for (IConfigurationListener2 configListener : configListeners2) {209                configListener.onConfigurationSkip(itr);210            }211        }212    }213214    /**215     * [IConfigurationListener2]216     * Invoked before a configuration method is invoked.217     * 218     * @param tr test result object for the associated configuration method219     */220    @Override221    public void beforeConfiguration(ITestResult tr) {222        synchronized(configListeners2) {223            for (IConfigurationListener2 configListener : Lists.reverse(configListeners2)) {224                configListener.beforeConfiguration(tr);225            }226        }227    }228229    /**230     * [ITestListener]231     * Invoked each time a test is skipped.232     *233     * @param result {@code ITestResult} containing information about the run test234     * @see ITestResult#SKIP235     */236    @Override237    public void onTestSkipped(ITestResult result) {238        239        // >>>>> ENTER workaround for TestNG bug240        // https://github.com/cbeust/testng/issues/1602241        ITestContext context = result.getTestContext();242        IInvokedMethod method = new InvokedMethod(243                        result.getTestClass(), result.getMethod(), System.currentTimeMillis(), result);244        245        beforeInvocation(method, result, context);246        // <<<<< LEAVE workaround for TestNG bug247        248        super.onTestSkipped(result);249    }250251    @Override252    protected void initialize() {253        super.initialize();254        annotationXformers2 = new ArrayList<>();255        annotationXformers3 = new ArrayList<>();256        configListeners2 = new ArrayList<>();257        methodListeners2 = new ArrayList<>();258    }259    260    @Override261    protected void bucketizeListener(ITestNGListener object) {262        super.bucketizeListener(object);263        264        if (object instanceof IAnnotationTransformer3) {265            synchronized(annotationXformers3) {266                annotationXformers3.add((IAnnotationTransformer3) object);267            }268        } else if (object instanceof IAnnotationTransformer2) {269            synchronized(annotationXformers2) {270                annotationXformers2.add((IAnnotationTransformer2) object);271            }272        } else if (object instanceof IAnnotationTransformer) {273            synchronized(annotationXformers) {274                annotationXformers.add((IAnnotationTransformer) object);275            }276        }277        278        if (object instanceof IConfigurationListener2) {279            synchronized(configListeners2) {280                configListeners2.add((IConfigurationListener2) object);281            }282        } else if (object instanceof IConfigurationListener) {283            synchronized(configListeners) {284                configListeners.add((IConfigurationListener) object);285            }286        }287        288        if (object instanceof IInvokedMethodListener2) {289            synchronized(methodListeners2) {290                methodListeners2.add((IInvokedMethodListener2) object);291            }292        } else if (object instanceof IInvokedMethodListener) {293            synchronized(methodListeners) {294                methodListeners.add((IInvokedMethodListener) object);295            }296        }297    }298}
...Source:TestNGListenerAdapterFactory.java  
1/*2 * Copyright 2011 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 *      http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package org.gradle.api.internal.tasks.testing.testng;17import org.gradle.util.ReflectionUtil;18import org.testng.ITestListener;19import java.lang.reflect.InvocationHandler;20import java.lang.reflect.Method;21import java.lang.reflect.Proxy;22class TestNGListenerAdapterFactory {23    private final ClassLoader classLoader;24    TestNGListenerAdapterFactory(ClassLoader classLoader) {25        this.classLoader = classLoader;26    }27    public ITestListener createAdapter(ITestListener listener) {28        Class<?> testNG6Class = tryLoadClass("org.testng.IConfigurationListener2");29        if (testNG6Class != null) {30            return createProxy(testNG6Class, listener);31        }32        Class<?> testNG5Class = tryLoadClass("org.testng.internal.IConfigurationListener");33        if (testNG5Class != null) {34            return createProxy(testNG5Class, listener);35        }36        throw new UnsupportedOperationException("Neither found interface 'org.testng.IConfigurationListener2' nor interface 'org.testng.internal.IConfigurationListener'. Which version of TestNG are you using?");37    }38    39    private Class<?> tryLoadClass(String name) {40        try {41            return classLoader.loadClass(name);42        } catch (ClassNotFoundException e) {43            return null;44        }45    }46    47    private ITestListener createProxy(Class<?> configListenerClass, final ITestListener listener) {48        return (ITestListener) Proxy.newProxyInstance(classLoader, new Class<?>[] {ITestListener.class, configListenerClass}, new InvocationHandler() {49            public Object invoke(Object proxy, Method method, Object[] args) {50                return ReflectionUtil.invoke(listener, method.getName(), args);51            }52        });53    }54}...Source:IResultListener2.java  
1package org.testng.internal;2import org.testng.IConfigurationListener2;3public interface IResultListener2 extends IResultListener, IConfigurationListener2 {4}...Interface IConfigurationListener2
Using AI Code Generation
1import org.testng.IConfigurationListener2;2import org.testng.ITestContext;3import org.testng.ITestResult;4public class MyConfigurationListener implements IConfigurationListener2 {5  public void onConfigurationSuccess(ITestResult itr) {6    System.out.println("onConfigurationSuccess(ITestResult itr) method invoked.");7  }8  public void onConfigurationFailure(ITestResult itr) {9    System.out.println("onConfigurationFailure(ITestResult itr) method invoked.");10  }11  public void onConfigurationSkip(ITestResult itr) {12    System.out.println("onConfigurationSkip(ITestResult itr) method invoked.");13  }14  public void beforeConfiguration(ITestResult tr) {15    System.out.println("beforeConfiguration(ITestResult tr) method invoked.");16  }17  public void onConfigurationStart(ITestResult tr) {18    System.out.println("onConfigurationStart(ITestResult tr) method invoked.");19  }20  public void onConfigurationFinish(ITestResult tr) {21    System.out.println("onConfigurationFinish(ITestResult tr) method invoked.");22  }23}24onConfigurationFinish(ITestResult trInterface IConfigurationListener2
Using AI Code Generation
1import org.testng.IConfigurationListener2;2import org.testng.ITestResult;3public class MyConfigurationListener2 implements IConfigurationListener2 {4    public void beforeConfiguration(ITestResult tr) {5        System.out.println("Before configuration " + tr.getMethod().getMethodName());6    }7    public void onConfigurationSuccess(ITestResult itr) {8        System.out.println("On configuration success " + itr.getMethod().getMethodName());9    }10    public void onConfigurationFailure(ITestResult itr) {11        System.out.println("On configuration failure " + itr.getMethod().getMethodName());12    }13    public void onConfigurationSkip(ITestResult itr) {14        System.out.println("On configuration skip " + itr.getMethod().getMethodName());15    }16}17import org.testng.IInvokedMethodListener2;18import org.testng.IInvokedMethod;19import org.testng.ITestResult;20public class MyInvokedMethodListener2 implements IInvokedMethodListener2 {21    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {22        System.out.println("Before invocation " + testResult.getMethod().getMethodName());23    }24    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {25        System.out.println("After invocation " + testResult.getMethod().getMethodName());26    }27    public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {28        System.out.println("Before invocation " + testResult.getMethod().getMethodName());29    }30    public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {31        System.out.println("After invocation " + testResult.getMethod().getMethodName());32    }33}34import org.testng.ITestListener2;35import org.testng.ITestResult;36public class MyTestListener2 implements ITestListener2 {37    public void onTestStart(ITestResult result) {38        System.out.println("On test start " + result.getMethod().getMethodName());39    }40    public void onTestSuccess(ITestResult result) {41        System.out.println("On test success " + result.getMethod().getMethodName());Interface IConfigurationListener2
Using AI Code Generation
1public class TestNGListener implements IConfigurationListener2 {2    public void beforeConfiguration(ITestResult result) {3        System.out.println("Before Configuration");4    }5    public void onConfigurationSuccess(ITestResult result) {6        System.out.println("On Configuration Success");7    }8    public void onConfigurationFailure(ITestResult result) {9        System.out.println("On Configuration Failure");10    }11    public void onConfigurationSkip(ITestResult result) {12        System.out.println("On Configuration Skip");13    }14}15public class TestNGListener implements ITestListener {16    public void onTestStart(ITestResult result) {17        System.out.println("On Test Start");18    }19    public void onTestSuccess(ITestResult result) {20        System.out.println("On Test Success");21    }22    public void onTestFailure(ITestResult result) {23        System.out.println("On Test Failure");24    }25    public void onTestSkipped(ITestResult result) {26        System.out.println("On Test Skipped");27    }28    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {29        System.out.println("On Test Failed But Within Success Percentage");30    }31    public void onStart(ITestContext context) {32        System.out.println("On Start");33    }34    public void onFinish(ITestContext context) {35        System.out.println("On Finish");36    }37}38public class TestNGListener implements ISuiteListener {39    public void onStart(ISuite suite) {40        System.out.println("On Suite Start");41    }42    public void onFinish(ISuite suite) {43        System.out.println("On Suite Finish");44    }45}46public class TestNGListener implements IInvokedMethodListener {47    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {48        System.out.println("Before Invocation");49    }50    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {51        System.out.println("After Invocation");Interface IConfigurationListener2
Using AI Code Generation
1public class TestNGListener implements IConfigurationListener2 {2    public void onConfigurationSuccess(ITestResult itr) {3        System.out.println("Test Passed");4    }5    public void onConfigurationFailure(ITestResult itr) {6        System.out.println("Test Failed");7    }8    public void onConfigurationSkip(ITestResult itr) {9        System.out.println("Test Skipped");10    }11    public void beforeConfiguration(ITestResult itr) {12        System.out.println("Before Test");13    }14    public void onConfigurationStart(ITestResult itr) {15        System.out.println("Test Started");16    }17    public void onConfigurationFinish(ITestResult itr) {18        System.out.println("Test Finished");19    }20}21public class TestNGListener implements ITestListener {22    public void onTestStart(ITestResult result) {23        System.out.println("Test Started");24    }25    public void onTestSuccess(ITestResult result) {26        System.out.println("Test Passed");27    }28    public void onTestFailure(ITestResult result) {29        System.out.println("Test Failed");30    }31    public void onTestSkipped(ITestResult result) {32        System.out.println("Test Skipped");33    }34    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {35        System.out.println("Test Failed But Within Success Percentage");36    }37    public void onStart(ITestContext context) {38        System.out.println("Test Started");39    }40    public void onFinish(ITestContext context) {41        System.out.println("Test Finished");42    }43}44public class TestNGListener implements IInvokedMethodListener {45    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {46        System.out.println("Before Test");47    }48    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {49        System.out.println("After Test");50    }51}52public class TestNGListener implements IMethodInterceptor {Interface IConfigurationListener2
Using AI Code Generation
1public class TestNGListener implements IConfigurationListener2 {2    public void onConfigurationSuccess(ITestResult itr) {3        System.out.println("onConfigurationSuccess method " + getTestMethodName(itr) + " succeed");4    }5    public void onConfigurationFailure(ITestResult itr) {6        System.out.println("onConfigurationFailure method " + getTestMethodName(itr) + " failed");7    }8    public void onConfigurationSkip(ITestResult itr) {9        System.out.println("onConfigurationSkip method " + getTestMethodName(itr) + " skipped");10    }11    public void beforeConfiguration(ITestResult tr) {12        System.out.println("beforeConfiguration " + getTestMethodName(tr));13    }14    public void onConfigurationStart(ITestResult tr) {15        System.out.println("onConfigurationStart " + getTestMethodName(tr));16    }17    public void onConfigurationFinish(ITestResult tr) {18        System.out.println("onConfigurationFinish " + getTestMethodName(tr));19    }20    private static String getTestMethodName(ITestResult iTestResult) {21        return iTestResult.getMethod().getConstructorOrMethod().getName();22    }23}24public class TestNGAnnotationTransformer implements IAnnotationTransformer2 {25    public void transform(IConfigurationAnnotation iConfigurationAnnotation, Class aClass, Constructor constructor, Method method) {26        iConfigurationAnnotation.setRetryAnalyzer(RetryAnalyzer.class);27    }28    public void transform(IDataProviderAnnotation iDataProviderAnnotation, Method method) {29    }30    public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {31    }32}33public class TestNGAnnotationTransformer implements IAnnotationTransformer2 {34    public void transform(IConfigurationAnnotation iConfigurationAnnotation, Class aClass, Constructor constructor, Method method) {35        iConfigurationAnnotation.setRetryAnalyzer(RetryAnalyzer.class);36    }37    public void transform(IDataProviderAnnotation iDataProviderAnnotation, Method method) {38    }39    public void transform(ITestAnnotation iTestAnnotation, Class aClass, Constructor constructor, Method method) {40    }41}Interface IConfigurationListener2
Using AI Code Generation
1import org.testng.IConfigurationListener2;2import org.testng.IConfigurationMethod;3import org.testng.ITestResult;4import org.testng.annotations.Test;5import org.testng.xml.XmlTest;6public class TestNGListenerExample implements IConfigurationListener2 {7	public void beforeConfiguration(IConfigurationMethod method, ITestResult testResult) {8		System.out.println("Before configuration method: " + method.getMethodName());9	}10	public void onConfigurationSuccess(IConfigurationMethod method, ITestResult testResult) {11		System.out.println("On configuration success: " + method.getMethodName());12	}13	public void onConfigurationFailure(IConfigurationMethod method, ITestResult testResult) {14		System.out.println("On configuration failure: " + method.getMethodName());15	}16	public void onConfigurationSkip(IConfigurationMethod method, ITestResult testResult) {17		System.out.println("On configuration skip: " + method.getMethodName());18	}19	public void testMethod() {20		System.out.println("I am inside test method.");21	}22}Interface IConfigurationListener2
Using AI Code Generation
1public class TestNGListener implements IConfigurationListener2 {2    public void onConfigurationSuccess(ITestResult itr) {3    }4    public void onConfigurationFailure(ITestResult itr) {5    }6    public void onConfigurationSkip(ITestResult itr) {7    }8}Interface IConfigurationListener2
Using AI Code Generation
1import org.testng.IConfigurationListener2;2import org.testng.ITestResult;3import org.testng.xml.XmlSuite;4public class TestNGListener implements IConfigurationListener2 {5    public void onConfigurationSuccess(ITestResult itr) {6        System.out.println("onConfigurationSuccess");7        System.out.println("Test Name: "+itr.getName());8    }9    public void onConfigurationFailure(ITestResult itr) {10        System.out.println("onConfigurationFailure");11        System.out.println("Test Name: "+itr.getName());12    }13    public void onConfigurationSkip(ITestResult itr) {14        System.out.println("onConfigurationSkip");15        System.out.println("Test Name: "+itr.getName());16    }17    public void beforeConfiguration(ITestResult itr) {18        System.out.println("beforeConfiguration");19        System.out.println("Test Name: "+itr.getName());20    }21    public void onStart(XmlSuite xs) {22        System.out.println("onStart");23        System.out.println("Test Name: "+xs.getName());24    }25    public void onFinish(XmlSuite xs) {26        System.out.println("onFinish");27        System.out.println("Test Name: "+xs.getName());28    }29}30import org.testng.ITestContext;31import org.testng.ITestListener;32import org.testng.ITestResult;33public class TestNGListener implements ITestListener {34    public void onTestStart(ITestResult itr) {35        System.out.println("onTestStart");36        System.out.println("Test Name: "+itr.getName());37    }38    public void onTestSuccess(ITestResult itr) {39        System.out.println("onTestSuccess");40        System.out.println("Test Name: "+itr.getName());41    }42    public void onTestFailure(ITestResult itr) {43        System.out.println("onTestFailure");44        System.out.println("Test Name: "+itr.getName());45    }46    public void onTestSkipped(ITestResult itr) {47        System.out.println("onTestSkipped");48        System.out.println("Test Name: "+itr.getName());49    }50    public void onTestFailedButWithinSuccessPercentage(ITestResult itrTestNG 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!!
