Best Testng code snippet using org.testng.TestRunner.getConfigurationListeners
Source:TestRunner.java  
...292      initMethods();293    }294    initListeners();295    addConfigurationListener(m_confListener);296    for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {297      addConfigurationListener(cl);298    }299  }300  private void initListeners() {301    //302    // Find all the listener factories and collect all the listeners requested in a303    // @Listeners annotation.304    //305    Set<Class<? extends ITestNGListener>> listenerClasses = Sets.newHashSet();306    Class<? extends ITestNGListenerFactory> listenerFactoryClass = null;307    for (IClass cls : getTestClasses()) {308      Class<?> realClass = cls.getRealClass();309      TestListenerHelper.ListenerHolder listenerHolder =310          TestListenerHelper.findAllListeners(realClass, m_annotationFinder);311      if (listenerFactoryClass == null) {312        listenerFactoryClass = listenerHolder.getListenerFactoryClass();313      }314      listenerClasses.addAll(listenerHolder.getListenerClasses());315    }316    if (listenerFactoryClass == null) {317      listenerFactoryClass = DefaultListenerFactory.class;318    }319    //320    // Now we have all the listeners collected from @Listeners and at most one321    // listener factory collected from a class implementing ITestNGListenerFactory.322    // Instantiate all the requested listeners.323    //324    ITestNGListenerFactory factory =325        TestListenerHelper.createListenerFactory(m_testClassFinder, listenerFactoryClass);326    // Instantiate all the listeners327    for (Class<? extends ITestNGListener> c : listenerClasses) {328      if (IClassListener.class.isAssignableFrom(c) && m_classListeners.containsKey(c)) {329        continue;330      }331      ITestNGListener listener = factory.createListener(c);332      addListener(listener);333    }334  }335  /** Initialize meta groups */336  private void initMetaGroups(XmlTest xmlTest) {337    Map<String, List<String>> metaGroups = xmlTest.getMetaGroups();338    for (Map.Entry<String, List<String>> entry : metaGroups.entrySet()) {339      addMetaGroup(entry.getKey(), entry.getValue());340    }341  }342  private void initRunInfo(final XmlTest xmlTest) {343    // Groups344    m_xmlMethodSelector.setIncludedGroups(createGroups(m_xmlTest.getIncludedGroups()));345    m_xmlMethodSelector.setExcludedGroups(createGroups(m_xmlTest.getExcludedGroups()));346    m_xmlMethodSelector.setScript(m_xmlTest.getScript());347    // Groups override348    m_xmlMethodSelector.setOverrideIncludedMethods(m_configuration.getOverrideIncludedMethods());349    // Methods350    m_xmlMethodSelector.setXmlClasses(m_xmlTest.getXmlClasses());351    m_runInfo.addMethodSelector(m_xmlMethodSelector, 10);352    // Add user-specified method selectors (only class selectors, we can ignore353    // script selectors here)354    if (null != xmlTest.getMethodSelectors()) {355      for (org.testng.xml.XmlMethodSelector selector : xmlTest.getMethodSelectors()) {356        if (selector.getClassName() != null) {357          IMethodSelector s = InstanceCreator.createSelector(selector);358          m_runInfo.addMethodSelector(s, selector.getPriority());359        }360      }361    }362  }363  private void initMethods() {364    //365    // Calculate all the methods we need to invoke366    //367    List<ITestNGMethod> beforeClassMethods = Lists.newArrayList();368    List<ITestNGMethod> testMethods = Lists.newArrayList();369    List<ITestNGMethod> afterClassMethods = Lists.newArrayList();370    List<ITestNGMethod> beforeSuiteMethods = Lists.newArrayList();371    List<ITestNGMethod> afterSuiteMethods = Lists.newArrayList();372    List<ITestNGMethod> beforeXmlTestMethods = Lists.newArrayList();373    List<ITestNGMethod> afterXmlTestMethods = Lists.newArrayList();374    ClassInfoMap classMap = new ClassInfoMap(m_testClassesFromXml);375    m_testClassFinder =376        new TestNGClassFinder(377            classMap, Maps.newHashMap(), m_configuration, this, holder);378    ITestMethodFinder testMethodFinder =379        new TestNGMethodFinder(m_runInfo, m_annotationFinder, comparator);380    m_runInfo.setTestMethods(testMethods);381    //382    // Initialize TestClasses383    //384    IClass[] classes = m_testClassFinder.findTestClasses();385    for (IClass ic : classes) {386      // Create TestClass387      ITestClass tc =388          new TestClass(389              ic,390              testMethodFinder,391              m_annotationFinder,392              m_xmlTest,393              classMap.getXmlClass(ic.getRealClass()), m_testClassFinder.getFactoryCreationFailedMessage());394      m_classMap.put(ic.getRealClass(), tc);395    }396    //397    // Calculate groups methods398    //399    Map<String, List<ITestNGMethod>> beforeGroupMethods =400        MethodGroupsHelper.findGroupsMethods(m_classMap.values(), true);401    Map<String, List<ITestNGMethod>> afterGroupMethods =402        MethodGroupsHelper.findGroupsMethods(m_classMap.values(), false);403    //404    // Walk through all the TestClasses, store their method405    // and initialize them with the correct ITestClass406    //407    for (ITestClass tc : m_classMap.values()) {408      fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);409      fixMethodsWithClass(((ITestClassConfigInfo) tc).getAllBeforeClassMethods().toArray(new ITestNGMethod[0]), tc, beforeClassMethods);410      fixMethodsWithClass(tc.getBeforeTestMethods(), tc, null);411      fixMethodsWithClass(tc.getAfterTestMethods(), tc, null);412      fixMethodsWithClass(tc.getAfterClassMethods(), tc, afterClassMethods);413      fixMethodsWithClass(tc.getBeforeSuiteMethods(), tc, beforeSuiteMethods);414      fixMethodsWithClass(tc.getAfterSuiteMethods(), tc, afterSuiteMethods);415      fixMethodsWithClass(tc.getBeforeTestConfigurationMethods(), tc, beforeXmlTestMethods);416      fixMethodsWithClass(tc.getAfterTestConfigurationMethods(), tc, afterXmlTestMethods);417      fixMethodsWithClass(418          tc.getBeforeGroupsMethods(),419          tc,420          MethodHelper.uniqueMethodList(beforeGroupMethods.values()));421      fixMethodsWithClass(422          tc.getAfterGroupsMethods(),423          tc,424          MethodHelper.uniqueMethodList(afterGroupMethods.values()));425    }426    //427    // Sort the methods428    //429    m_beforeSuiteMethods =430        MethodHelper.collectAndOrderMethods(431            beforeSuiteMethods,432            false /* forTests */,433            m_runInfo,434            m_annotationFinder,435            true /* unique */,436            m_excludedMethods,437            comparator);438    m_beforeXmlTestMethods =439        MethodHelper.collectAndOrderMethods(440            beforeXmlTestMethods,441            false /* forTests */,442            m_runInfo,443            m_annotationFinder,444            true /* unique (CQ added by me)*/,445            m_excludedMethods,446            comparator);447    m_classMethodMap = new ClassMethodMap(Arrays.asList(testMethodsContainer.getItems()), m_xmlMethodSelector);448    m_groupMethods = new ConfigurationGroupMethods(testMethodsContainer, beforeGroupMethods, afterGroupMethods);449    m_afterXmlTestMethods =450        MethodHelper.collectAndOrderMethods(451            afterXmlTestMethods,452            false /* forTests */,453            m_runInfo,454            m_annotationFinder,455            true /* unique (CQ added by me)*/,456            m_excludedMethods,457            comparator);458    m_afterSuiteMethods =459        MethodHelper.collectAndOrderMethods(460            afterSuiteMethods,461            false /* forTests */,462            m_runInfo,463            m_annotationFinder,464            true /* unique */,465            m_excludedMethods,466            comparator);467  }468  private ITestNGMethod[] computeAndGetAllTestMethods() {469    List<ITestNGMethod> testMethods = Lists.newArrayList();470    for (ITestClass tc : m_classMap.values()) {471      fixMethodsWithClass(tc.getTestMethods(), tc, testMethods);472    }473    return MethodHelper.collectAndOrderMethods(474        testMethods,475        true /* forTest? */,476        m_runInfo,477        m_annotationFinder,478        false /* unique */,479        m_excludedMethods,480        comparator);481  }482  public Collection<ITestClass> getTestClasses() {483    return m_classMap.values();484  }485  public void setTestName(String name) {486    m_testName = name;487  }488  public void setOutputDirectory(String od) {489    m_outputDirectory = od;490  }491  private void addMetaGroup(String name, List<String> groupNames) {492    m_metaGroups.put(name, groupNames);493  }494  private Map<String, String> createGroups(List<String> groups) {495    return GroupsHelper.createGroups(m_metaGroups, groups);496  }497  /**498   * The main entry method for TestRunner.499   *500   * <p>This is where all the hard work is done: - Invoke configuration methods - Invoke test501   * methods - Catch exceptions - Collect results - Invoke listeners - etc...502   */503  public void run() {504    beforeRun();505    try {506      XmlTest test = getTest();507      if (test.isJUnit()) {508        privateRunJUnit();509      } else {510        privateRun(test);511      }512    } finally {513      afterRun();514      forgetHeavyReferencesIfNeeded();515    }516  }517  private void forgetHeavyReferencesIfNeeded() {518    if (RuntimeBehavior.isMemoryFriendlyMode()) {519      testMethodsContainer.clearItems();520      m_groupMethods = null;521      m_classMethodMap = null;522    }523  }524  /** Before run preparements. */525  private void beforeRun() {526    //527    // Log the start date528    //529    m_startDate = new Date(System.currentTimeMillis());530    // Log start531    logStart();532    // Invoke listeners533    fireEvent(true /*start*/);534    // invoke @BeforeTest535    ITestNGMethod[] testConfigurationMethods = getBeforeTestConfigurationMethods();536    invokeTestConfigurations(testConfigurationMethods);537  }538  private void invokeTestConfigurations(ITestNGMethod[] testConfigurationMethods) {539    if (null != testConfigurationMethods && testConfigurationMethods.length > 0) {540      ConfigMethodArguments arguments = new Builder()541          .usingConfigMethodsAs(testConfigurationMethods)542          .forSuite(m_xmlTest.getSuite())543          .usingParameters(m_xmlTest.getAllParameters())544          .build();545      m_invoker.getConfigInvoker().invokeConfigurations(arguments);546    }547  }548  private ITestNGMethod[] m_allJunitTestMethods = new ITestNGMethod[] {};549  private void privateRunJUnit() {550    final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false);551    final Set<Class<?>> classes = cim.getClasses();552    final List<ITestNGMethod> runMethods = Lists.newArrayList();553    List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();554    // FIXME: directly referencing JUnitTestRunner which uses JUnit classes555    // may result in an class resolution exception under different JVMs556    // The resolution process is not specified in the JVM spec with a specific implementation,557    // so it can be eager => failure558    workers.add(559        new IWorker<ITestNGMethod>() {560          /** @see TestMethodWorker#getTimeOut() */561          @Override562          public long getTimeOut() {563            return 0;564          }565          /** @see java.lang.Runnable#run() */566          @Override567          public void run() {568            for (Class<?> tc : classes) {569              List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods();570              List<String> methods = Lists.newArrayList();571              for (XmlInclude inc : includedMethods) {572                methods.add(inc.getName());573              }574              IJUnitTestRunner tr = IJUnitTestRunner.createTestRunner(TestRunner.this);575              tr.setInvokedMethodListeners(m_invokedMethodListeners);576              try {577                tr.run(tc, methods.toArray(new String[0]));578              } catch (Exception ex) {579                LOGGER.error(ex.getMessage(), ex);580              } finally {581                runMethods.addAll(tr.getTestMethods());582              }583            }584          }585          @Override586          public List<ITestNGMethod> getTasks() {587            throw new TestNGException("JUnit not supported");588          }589          @Override590          public int getPriority() {591            if (m_allJunitTestMethods.length == 1) {592              return m_allJunitTestMethods[0].getPriority();593            } else {594              return 0;595            }596          }597          @Override598          public int compareTo(@Nonnull IWorker<ITestNGMethod> other) {599            return getPriority() - other.getPriority();600          }601        });602    runJUnitWorkers(workers);603    m_allJunitTestMethods = runMethods.toArray(new ITestNGMethod[0]);604  }605  private static Comparator<ITestNGMethod> newComparator(boolean needPrioritySort) {606    return needPrioritySort ? new TestMethodComparator() : null;607  }608  private boolean sortOnPriority(ITestNGMethod[] interceptedOrder) {609    return m_methodInterceptors.size() > 1 ||610        Arrays.stream(interceptedOrder).anyMatch(m -> m.getPriority() != 0);611  }612  // If any of the test methods specify a priority other than the default, we'll need to be able to sort them.613  private static BlockingQueue<Runnable> newQueue(boolean needPrioritySort) {614    return needPrioritySort ? new PriorityBlockingQueue<>() : new LinkedBlockingQueue<>();615  }616  /**617   * Main method that create a graph of methods and then pass it to the graph executor to run them.618   */619  private void privateRun(XmlTest xmlTest) {620    boolean parallel = xmlTest.getParallel().isParallel();621    // parallel622    int threadCount = parallel ? xmlTest.getThreadCount() : 1;623    // Make sure we create a graph based on the intercepted methods, otherwise an interceptor624    // removing methods would cause the graph never to terminate (because it would expect625    // termination from methods that never get invoked).626    ITestNGMethod[] interceptedOrder = intercept(getAllTestMethods());627    AtomicReference<IDynamicGraph<ITestNGMethod>> reference = new AtomicReference<>();628    TimeUtils.computeAndShowTime("DynamicGraphHelper.createDynamicGraph()",629        () -> {630          IDynamicGraph<ITestNGMethod> ref = DynamicGraphHelper631              .createDynamicGraph(interceptedOrder, getCurrentXmlTest());632          reference.set(ref);633        }634    );635    IDynamicGraph<ITestNGMethod> graph = reference.get();636    graph.setVisualisers(this.visualisers);637    // In some cases, additional sorting is needed to make sure tests run in the appropriate order.638    // If the user specified a method interceptor, or if we have any methods that have a non-default639    // priority on them, we need to sort.640    boolean needPrioritySort = sortOnPriority(interceptedOrder);641    Comparator<ITestNGMethod> methodComparator = newComparator(needPrioritySort);642    if (parallel) {643      if (graph.getNodeCount() <= 0) {644        return;645      }646      ITestNGThreadPoolExecutor executor =647          this.m_configuration.getExecutorFactory().newTestMethodExecutor(648              "test=" + xmlTest.getName(),649              graph,650              this,651              threadCount,652              threadCount,653              0,654              TimeUnit.MILLISECONDS,655              newQueue(needPrioritySort),656              methodComparator);657      executor.run();658      try {659        long timeOut = m_xmlTest.getTimeOut(XmlTest.DEFAULT_TIMEOUT_MS);660        Utils.log(661            "TestRunner",662            2,663            "Starting executor for test "664                + m_xmlTest.getName()665                + " with time out:"666                + timeOut667                + " milliseconds.");668        executor.awaitTermination(timeOut, TimeUnit.MILLISECONDS);669        executor.shutdownNow();670      } catch (InterruptedException handled) {671        LOGGER.error(handled.getMessage(), handled);672        Thread.currentThread().interrupt();673      }674      return;675    }676    List<ITestNGMethod> freeNodes = graph.getFreeNodes();677    if (graph.getNodeCount() > 0 && freeNodes.isEmpty()) {678      throw new TestNGException("No free nodes found in:" + graph);679    }680    while (!freeNodes.isEmpty()) {681      if (needPrioritySort) {682        freeNodes.sort(methodComparator);683        // Since this is sequential, let's run one at a time and fetch/sort freeNodes after each method.684        // Future task: To optimize this, we can only update freeNodes after running a test that another test is dependent upon.685        freeNodes = freeNodes.subList(0, 1);686      }687      createWorkers(freeNodes).forEach(Runnable::run);688      graph.setStatus(freeNodes, IDynamicGraph.Status.FINISHED);689      freeNodes = graph.getFreeNodes();690    }691  }692  /** Apply the method interceptor (if applicable) to the list of methods. */693  private ITestNGMethod[] intercept(ITestNGMethod[] methods) {694    List<IMethodInstance> methodInstances =695        MethodHelper.methodsToMethodInstances(Arrays.asList(methods));696    for (IMethodInterceptor m_methodInterceptor : m_methodInterceptors) {697      methodInstances = m_methodInterceptor.intercept(methodInstances, this);698    }699    List<ITestNGMethod> result = MethodHelper.methodInstancesToMethods(methodInstances);700    // Since an interceptor is involved, we would need to ensure that the ClassMethodMap object is701    // in sync with the702    // output of the interceptor, else @AfterClass doesn't get executed at all when interceptors are703    // involved.704    // so let's update the current classMethodMap object with the list of methods obtained from the705    // interceptor.706    this.m_classMethodMap = new ClassMethodMap(result, null);707    ITestNGMethod[] resultArray = result.toArray(new ITestNGMethod[0]);708    // Check if an interceptor had altered the effective test method count. If yes, then we need to709    // update our configurationGroupMethod object with that information.710    if (resultArray.length != testMethodsContainer.getItems().length) {711      m_groupMethods =712          new ConfigurationGroupMethods(713              new TestMethodContainer(() -> resultArray),714              m_groupMethods.getBeforeGroupsMethods(),715              m_groupMethods.getAfterGroupsMethods());716    }717    // If the user specified a method interceptor, whatever that returns is the order we're going718    // to run things in. Set the intercepted priority for that case.719    // There's a built-in interceptor, so look for more than one.720    if (m_methodInterceptors.size() > 1) {721      for (int i = 0; i < resultArray.length; ++i) {722        resultArray[i].setInterceptedPriority(i);723      }724    }725    return resultArray;726  }727  /**728   * Create a list of workers to run the methods passed in parameter. Each test method is run in its729   * own worker except in the following cases: - The method belongs to a class that730   * has @Test(sequential=true) - The parallel attribute is set to "classes" In both these cases,731   * all the methods belonging to that class will then be put in the same worker in order to run in732   * the same thread.733   */734  @Override735  public List<IWorker<ITestNGMethod>> createWorkers(List<ITestNGMethod> methods) {736    AbstractParallelWorker.Arguments args =737        new AbstractParallelWorker.Arguments.Builder()738            .classMethodMap(this.m_classMethodMap)739            .configMethods(this.m_groupMethods)740            .finder(this.m_annotationFinder)741            .invoker(this.m_invoker)742            .methods(methods)743            .testContext(this)744            .listeners(this.m_classListeners.values())745            .build();746    return AbstractParallelWorker.newWorker(m_xmlTest.getParallel(), m_xmlTest.getGroupByInstances()).createWorkers(args);747  }748  //749  // Invoke the workers750  //751  private void runJUnitWorkers(List<? extends IWorker<ITestNGMethod>> workers) {752    // Sequential run753    workers.forEach(Runnable::run);754  }755  private void afterRun() {756    // invoke @AfterTest757    ITestNGMethod[] testConfigurationMethods = getAfterTestConfigurationMethods();758    invokeTestConfigurations(testConfigurationMethods);759    //760    // Log the end date761    //762    m_endDate = new Date(System.currentTimeMillis());763    dumpInvokedMethods();764    // Invoke listeners765    fireEvent(false /*stop*/);766  }767  /** Logs the beginning of the {@link #beforeRun()} . */768  private void logStart() {769    log(770        "Running test "771            + m_testName772            + " on "773            + m_classMap.size()774            + " "775            + " classes, "776            + " included groups:["777            + Strings.valueOf(m_xmlMethodSelector.getIncludedGroups())778            + "] excluded groups:["779            + Strings.valueOf(m_xmlMethodSelector.getExcludedGroups())780            + "]");781    if (getVerbose() >= 3) {782      for (ITestClass tc : m_classMap.values()) {783        ((TestClass) tc).dump();784      }785    }786  }787  /**788   * Trigger the start/finish event.789   *790   * @param isStart <tt>true</tt> if the event is for start, <tt>false</tt> if the event is for791   *     finish792   */793  private void fireEvent(boolean isStart) {794    for (ITestListener itl : m_testListeners) {795      if (isStart) {796        itl.onStart(this);797      } else {798        itl.onFinish(this);799      }800    }801  }802  /////803  // ITestContext804  //805  @Override806  public String getName() {807    return m_testName;808  }809  /** @return Returns the startDate. */810  @Override811  public Date getStartDate() {812    return m_startDate;813  }814  /** @return Returns the endDate. */815  @Override816  public Date getEndDate() {817    return m_endDate;818  }819  @Override820  public IResultMap getPassedTests() {821    return m_passedTests;822  }823  @Override824  public IResultMap getSkippedTests() {825    return m_skippedTests;826  }827  @Override828  public IResultMap getFailedTests() {829    return m_failedTests;830  }831  @Override832  public IResultMap getFailedButWithinSuccessPercentageTests() {833    return m_failedButWithinSuccessPercentageTests;834  }835  @Override836  public String[] getIncludedGroups() {837    Map<String, String> ig = m_xmlMethodSelector.getIncludedGroups();838    return ig.values().toArray(new String[0]);839  }840  @Override841  public String[] getExcludedGroups() {842    Map<String, String> eg = m_xmlMethodSelector.getExcludedGroups();843    return eg.values().toArray(new String[0]);844  }845  @Override846  public String getOutputDirectory() {847    return m_outputDirectory;848  }849  /** @return Returns the suite. */850  @Override851  public ISuite getSuite() {852    return m_suite;853  }854  @Override855  public ITestNGMethod[] getAllTestMethods() {856    if (getTest().isJUnit()) {857      //This is true only when we are running JUnit mode858      return m_allJunitTestMethods;859    }860    return testMethodsContainer.getItems();861  }862  @Override863  public String getHost() {864    return m_host;865  }866  @Override867  public Collection<ITestNGMethod> getExcludedMethods() {868    Map<ITestNGMethod, ITestNGMethod> vResult = Maps.newHashMap();869    for (ITestNGMethod m : m_excludedMethods) {870      vResult.put(m, m);871    }872    return vResult.keySet();873  }874  /** @see org.testng.ITestContext#getFailedConfigurations() */875  @Override876  public IResultMap getFailedConfigurations() {877    return m_failedConfigurations;878  }879  @Override880  public IResultMap getConfigurationsScheduledForInvocation() {881    return m_configsToBeInvoked;882  }883  /** @see org.testng.ITestContext#getPassedConfigurations() */884  @Override885  public IResultMap getPassedConfigurations() {886    return m_passedConfigurations;887  }888  /** @see org.testng.ITestContext#getSkippedConfigurations() */889  @Override890  public IResultMap getSkippedConfigurations() {891    return m_skippedConfigurations;892  }893  @Override894  public void addPassedTest(ITestNGMethod tm, ITestResult tr) {895    m_passedTests.addResult(tr);896  }897  @Override898  public Set<ITestResult> getPassedTests(ITestNGMethod tm) {899    return m_passedTests.getResults(tm);900  }901  @Override902  public Set<ITestResult> getFailedTests(ITestNGMethod tm) {903    return m_failedTests.getResults(tm);904  }905  @Override906  public Set<ITestResult> getSkippedTests(ITestNGMethod tm) {907    return m_skippedTests.getResults(tm);908  }909  @Override910  public void addSkippedTest(ITestNGMethod tm, ITestResult tr) {911    m_skippedTests.addResult(tr);912  }913  @Override914  public void addFailedTest(ITestNGMethod testMethod, ITestResult result) {915    logFailedTest(result, false /* withinSuccessPercentage */);916  }917  @Override918  public void addFailedButWithinSuccessPercentageTest(919      ITestNGMethod testMethod, ITestResult result) {920    logFailedTest(result, true /* withinSuccessPercentage */);921  }922  @Override923  public XmlTest getTest() {924    return m_xmlTest;925  }926  @Override927  public List<ITestListener> getTestListeners() {928    return m_testListeners;929  }930  @Override931  public List<IConfigurationListener> getConfigurationListeners() {932    List<IConfigurationListener> listeners = Lists.newArrayList(m_configurationListeners);933    for (IConfigurationListener each : this.m_configuration.getConfigurationListeners()) {934      boolean duplicate = false;935      for (IConfigurationListener listener : listeners) {936        if (each.getClass().equals(listener.getClass())) {937          duplicate = true;938          break;939        }940      }941      if (!duplicate) {942        listeners.add(each);943      }944    }945    return Lists.newArrayList(listeners);946  }947  private void logFailedTest(ITestResult tr, boolean withinSuccessPercentage) {...Source:SuiteRunner.java  
...450      }451      for (ITestListener itl : m_failureGenerators) {452        testRunner.addListener(itl);453      }454      for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {455        testRunner.addConfigurationListener(cl);456      }457      return testRunner;458    }459  }460  private static class ProxyTestRunnerFactory implements ITestRunnerFactory {461    private ITestListener[] m_failureGenerators;462    private ITestRunnerFactory m_target;463    public ProxyTestRunnerFactory(ITestListener[] failureListeners, ITestRunnerFactory target) {464      m_failureGenerators = failureListeners;465      m_target= target;466    }467    @Override468    public TestRunner newTestRunner(ISuite suite, XmlTest test,...Source:JUnit4TestRunner.java  
...129                tr = createTestResult(failure.getDescription());130                tr.setStatus(TestResult.FAILURE);131                tr.setEndMillis(Calendar.getInstance().getTimeInMillis());132                tr.setThrowable(failure.getException());133                for (IConfigurationListener l : m_parentRunner.getConfigurationListeners()) {134                    l.onConfigurationFailure(tr);135                }136                for (Description childDesc : failure.getDescription().getChildren()) {137                    testIgnored(childDesc);138                }139            } else {140                tr.setStatus(TestResult.FAILURE);141                tr.setEndMillis(Calendar.getInstance().getTimeInMillis());142                tr.setThrowable(failure.getException());143                m_parentRunner.addFailedTest(tr.getMethod(), tr);144                for (ITestListener l : m_listeners) {145                    l.onTestFailure(tr);146                }147            }...Source:RemoteTestNG6_9_10.java  
...28            if (m_useDefaultListeners) {29              runner.addListener(new TestHTMLReporter());30              runner.addListener(new JUnitXMLReporter());31            }32            for (IConfigurationListener cl : getConfiguration().getConfigurationListeners()) {33              runner.addListener(cl);34            }35            return runner;36          }37        };38    }39    return m_customTestRunnerFactory;40  }41  @Override42  protected ITestRunnerFactory createDelegatingTestRunnerFactory(ITestRunnerFactory trf, MessageHub smsh) {43    return new DelegatingTestRunnerFactory(trf, smsh);44  }45  private static class DelegatingTestRunnerFactory implements ITestRunnerFactory {46    private final ITestRunnerFactory m_delegateFactory;...Source:RemoteTestNG6_5.java  
...26          if (m_useDefaultListeners) {27            runner.addListener(new TestHTMLReporter());28            runner.addListener(new JUnitXMLReporter());29          }30          for (IConfigurationListener cl : getConfiguration().getConfigurationListeners()) {31            runner.addListener(cl);32          }33          return runner;34        }35      };36    }37    return m_customTestRunnerFactory;38  }39  @Override40  protected ITestRunnerFactory createDelegatingTestRunnerFactory(ITestRunnerFactory trf, MessageHub smsh) {41    return new DelegatingTestRunnerFactory(trf, smsh);42  }43  private static class DelegatingTestRunnerFactory implements ITestRunnerFactory {44    private final ITestRunnerFactory m_delegateFactory;...Source:RemoteTestNG6_9_7.java  
...26            if (m_useDefaultListeners) {27              runner.addListener(new TestHTMLReporter());28              runner.addListener(new JUnitXMLReporter());29            }30            for (IConfigurationListener cl : getConfiguration().getConfigurationListeners()) {31              runner.addListener(cl);32            }33            return runner;34          }35        };36    }37    return m_customTestRunnerFactory;38  }39  @Override40  protected ITestRunnerFactory createDelegatingTestRunnerFactory(ITestRunnerFactory trf, MessageHub smsh) {41    return new DelegatingTestRunnerFactory(trf, smsh);42  }43  private static class DelegatingTestRunnerFactory implements ITestRunnerFactory {44    private final ITestRunnerFactory m_delegateFactory;...Source:020fb.java  
...13   transient private boolean m_skipFailedInvocationCounts;14@@ -1416,7 +1416,7 @@15 16   @Override17   public List<IConfigurationListener> getConfigurationListeners() {18-    return m_configurationListeners;19+    return Lists.<IConfigurationListener>newArrayList(m_configurationListeners);20   }21   //22   // ITestResultNotifier...Source:43b55.java  
...8       for (ITestListener itl : m_failureGenerators) {9-        testRunner.addListener(itl);10+        testRunner.addTestListener(itl);11       }12       for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) {13         testRunner.addConfigurationListener(cl);...getConfigurationListeners
Using AI Code Generation
1package com.automationrhapsody.testng;2import java.util.List;3import org.testng.IConfigurationListener;4import org.testng.ITestContext;5import org.testng.ITestResult;6import org.testng.TestListenerAdapter;7import org.testng.TestNG;8import org.testng.annotations.Test;9public class TestRunnerTest {10    public void testGetConfigurationListeners() {11        TestRunner testRunner = new TestRunner();12        List<IConfigurationListener> configurationListeners = testRunner.getConfigurationListeners();13        System.out.println("Configuration listeners: " + configurationListeners);14    }15    private static class TestRunner extends TestNG {16        public List<IConfigurationListener> getConfigurationListeners() {17            return super.getConfigurationListeners();18        }19    }20}21package com.automationrhapsody.testng;22import java.util.List;23import org.testng.IConfigurationListener;24import org.testng.ITestContext;25import org.testng.ITestResult;26import org.testng.TestListenerAdapter;27import org.testng.TestNG;28import org.testng.annotations.Test;29public class TestRunnerTest {30    public void testGetConfigurationListeners() {31        TestRunner testRunner = new TestRunner();32        List<IConfigurationListener> configurationListeners = testRunner.getConfigurationListeners();33        System.out.println("Configuration listeners: " + configurationListeners);34    }35    private static class TestRunner extends TestNG {36        public List<IConfigurationListener> getConfigurationListeners() {37            return super.getConfigurationListeners();38        }39    }40}41package com.automationrhapsody.testng;42import java.util.List;43import org.testng.IConfigurationListener;44import org.testng.ITestContext;45import org.testng.ITestResult;46import org.testng.TestListenerAdapter;47import org.testng.TestNG;48import org.testng.annotations.Test;49public class TestRunnerTest {50    public void testGetConfigurationListeners() {51        TestRunner testRunner = new TestRunner();52        List<IConfigurationListener> configurationListeners = testRunner.getConfigurationListeners();53        System.out.println("Configuration listeners: " + configurationListeners);54    }55    private static class TestRunner extends TestNG {56        public List<IConfigurationListener> getConfigurationListeners() {57            return super.getConfigurationListeners();58        }59    }60}61package com.automationrhapsody.testng;62import java.util.List;63import org.testng.IConfigurationListener;64import org.testng.ITestContext;65import org.testng.ITestResult;66import org.testng.TestListenerAdapter;67import org.testng.TestNG;68import org.testng.annotations.Test;getConfigurationListeners
Using AI Code Generation
1package com.test;2import java.util.List;3import org.testng.ITestNGListener;4import org.testng.TestRunner;5public class GetConfigurationListeners {6	public static void main(String[] args) {7		TestRunner testRunner = new TestRunner();8		List<ITestNGListener> list = testRunner.getConfigurationListeners();9		System.out.println(list);10	}11}getConfigurationListeners
Using AI Code Generation
1public void testConfigurationListeners() {2    TestRunner testRunner = new TestRunner();3    IConfigurationListener[] configurationListeners = testRunner.getConfigurationListeners();4    Assert.assertEquals(configurationListeners.length, 1);5}6public void testSetConfigurationListeners() {7    TestRunner testRunner = new TestRunner();8    IConfigurationListener[] configurationListeners = testRunner.getConfigurationListeners();9    testRunner.setConfigurationListeners(configurationListeners);10}11public void testAddConfigurationListener() {12    TestRunner testRunner = new TestRunner();13    IConfigurationListener[] configurationListeners = testRunner.getConfigurationListeners();14    testRunner.addConfigurationListener(configurationListeners[0]);15}16public void testRemoveConfigurationListener() {17    TestRunner testRunner = new TestRunner();18    IConfigurationListener[] configurationListeners = testRunner.getConfigurationListeners();19    testRunner.removeConfigurationListener(configurationListeners[0]);20}21public void testGetTestListeners() {22    TestRunner testRunner = new TestRunner();23    ITestListener[] testListeners = testRunner.getTestListeners();24    Assert.assertEquals(testListeners.length, 1);25}26public void testSetTestListeners() {27    TestRunner testRunner = new TestRunner();28    ITestListener[] testListeners = testRunner.getTestListeners();29    testRunner.setTestListeners(testListeners);30}31public void testAddTestListener() {32    TestRunner testRunner = new TestRunner();33    ITestListener[] testListeners = testRunner.getTestListeners();34    testRunner.addTestListener(testListeners[0]);35}36public void testRemoveTestListener() {37    TestRunner testRunner = new TestRunner();38    ITestListener[] testListeners = testRunner.getTestListeners();39    testRunner.removeTestListener(testListeners[0]);40}41public void testGetMethodInterceptors() {42    TestRunner testRunner = new TestRunner();getConfigurationListeners
Using AI Code Generation
1import java.util.List;2import java.util.Map;3import java.util.Set;4import org.testng.ITestContext;5import org.testng.ITestNGMethod;6import org.testng.TestRunner;7importTestNG 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!!
