Best Testng code snippet using org.testng.Interface ITestNGMethod.getConstructorOrMethod
Source:Invoker.java  
...165        if (inst == null) {166          inst = instance;167        }168        Class<?> objectClass= inst.getClass();169        ConstructorOrMethod method= tm.getConstructorOrMethod();170        // Only run the configuration if171        // - the test is enabled and172        // - the Configuration method belongs to the same class or a parent173        configurationAnnotation = AnnotationHelper.findConfiguration(m_annotationFinder, method);174        boolean alwaysRun = isAlwaysRun(configurationAnnotation);175        if(MethodHelper.isEnabled(objectClass, m_annotationFinder) || alwaysRun) {176          if (MethodHelper.isEnabled(configurationAnnotation)) {177            if (!confInvocationPassed(tm, currentTestMethod, testClass, instance) && !alwaysRun) {178              log(3, "Skipping " + Utils.detailedMethodName(tm, true));179              handleConfigurationSkip(tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);180              continue;181            }182            log(3, "Invoking " + Utils.detailedMethodName(tm, true));183            Object[] parameters = Parameters.createConfigurationParameters(tm.getConstructorOrMethod().getMethod(),184                params,185                parameterValues,186                currentTestMethod,187                m_annotationFinder,188                suite,189                m_testContext,190                testMethodResult);191            testResult.setParameters(parameters);192            runConfigurationListeners(testResult, true /* before */);193            Object newInstance;194            if (instance == null || !tm.getConstructorOrMethod().getDeclaringClass().isAssignableFrom(instance.getClass())) {195              newInstance = inst;196            } else {197              newInstance = instance;198            }199            invokeConfigurationMethod(newInstance, tm, parameters, testResult);200            runConfigurationListeners(testResult, false /* after */);201          }202          else {203            log(3,204                "Skipping "205                + Utils.detailedMethodName(tm, true)206                + " because it is not enabled");207          }208        } // if is enabled209        else {210          log(3,211              "Skipping "212              + Utils.detailedMethodName(tm, true)213              + " because "214              + objectClass.getName()215              + " is not enabled");216        }217      }218      catch(InvocationTargetException ex) {219        handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);220      } catch(Throwable ex) { // covers the non-wrapper exceptions221        handleConfigurationFailure(ex, tm, testResult, configurationAnnotation, currentTestMethod, instance, suite);222      }223    } // for methods224  }225  /**226   * Marks the current <code>TestResult</code> as skipped and invokes the listeners.227   */228  private void handleConfigurationSkip(ITestNGMethod tm,229                                       ITestResult testResult,230                                       IConfigurationAnnotation annotation,231                                       ITestNGMethod currentTestMethod,232                                       Object instance,233                                       XmlSuite suite) {234    recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);235    testResult.setStatus(ITestResult.SKIP);236    runConfigurationListeners(testResult, false /* after */);237  }238  /**239   * Is the <code>IConfiguration</code> marked as alwaysRun.240   */241  private boolean isAlwaysRun(IConfigurationAnnotation configurationAnnotation) {242    if(null == configurationAnnotation) {243      return false;244    }245    boolean alwaysRun= false;246    if ((configurationAnnotation.getAfterSuite()247        || configurationAnnotation.getAfterTest()248        || configurationAnnotation.getAfterTestClass()249        || configurationAnnotation.getAfterTestMethod()250        || configurationAnnotation.getBeforeTestMethod()251        || configurationAnnotation.getBeforeTestClass()252        || configurationAnnotation.getBeforeTest()253        || configurationAnnotation.getBeforeSuite())254        && configurationAnnotation.getAlwaysRun())255    {256        alwaysRun= true;257    }258    return alwaysRun;259  }260  private void handleConfigurationFailure(Throwable ite,261                                          ITestNGMethod tm,262                                          ITestResult testResult,263                                          IConfigurationAnnotation annotation,264                                          ITestNGMethod currentTestMethod,265                                          Object instance,266                                          XmlSuite suite)267  {268    Throwable cause= ite.getCause() != null ? ite.getCause() : ite;269    if(isSkipExceptionAndSkip(cause)) {270      testResult.setThrowable(cause);271      handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);272      return;273    }274    Utils.log("", 3, "Failed to invoke configuration method "275        + tm.getQualifiedName() + ":" + cause.getMessage());276    handleException(cause, tm, testResult, 1);277    runConfigurationListeners(testResult, false /* after */);278    //279    // If in TestNG mode, need to take a look at the annotation to figure out280    // what kind of @Configuration method we're dealing with281    //282    if (null != annotation) {283      recordConfigurationInvocationFailed(tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);284    }285  }286  /**287   * @return All the classes that belong to the same <test> tag as @param cls288   */289  private XmlClass[] findClassesInSameTest(Class<?> cls, XmlSuite suite) {290    Map<String, XmlClass> vResult= Maps.newHashMap();291    String className= cls.getName();292    for(XmlTest test : suite.getTests()) {293      for(XmlClass testClass : test.getXmlClasses()) {294        if(testClass.getName().equals(className)) {295          // Found it, add all the classes in this test in the result296          for(XmlClass thisClass : test.getXmlClasses()) {297            vResult.put(thisClass.getName(), thisClass);298          }299          // Note:  we need to iterate through the entire suite since the same300          // class might appear in several <test> tags301        }302      }303    }304    XmlClass[] result= vResult.values().toArray(new XmlClass[vResult.size()]);305    return result;306  }307  /**308   * Record internally the failure of a Configuration, so that we can determine309   * later if @Test should be skipped.310   */311  private void recordConfigurationInvocationFailed(ITestNGMethod tm,312                                                   IClass testClass,313                                                   IConfigurationAnnotation annotation,314                                                   ITestNGMethod currentTestMethod,315                                                   Object instance,316                                                   XmlSuite suite) {317    // If beforeTestClass or afterTestClass failed, mark either the config method's318    // entire class as failed, or the class under tests as failed, depending on319    // the configuration failure policy320    if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {321      // tm is the configuration method, and currentTestMethod is null for BeforeClass322      // methods, so we need testClass323      if (m_continueOnFailedConfiguration) {324        setClassInvocationFailure(testClass.getRealClass(), instance);325      } else {326        setClassInvocationFailure(tm.getRealClass(), instance);327      }328    }329    // If before/afterTestMethod failed, mark either the config method's entire330    // class as failed, or just the current test method as failed, depending on331    // the configuration failure policy332    else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {333      if (m_continueOnFailedConfiguration) {334        setMethodInvocationFailure(currentTestMethod, instance);335      } else {336        setClassInvocationFailure(tm.getRealClass(), instance);337      }338    }339    // If beforeSuite or afterSuite failed, mark *all* the classes as failed340    // for configurations.  At this point, the entire Suite is screwed341    else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {342      m_suiteState.failed();343    }344    // beforeTest or afterTest:  mark all the classes in the same345    // <test> stanza as failed for configuration346    else if (annotation.getBeforeTest() || annotation.getAfterTest()) {347      setClassInvocationFailure(tm.getRealClass(), instance);348      XmlClass[] classes= findClassesInSameTest(tm.getRealClass(), suite);349      for(XmlClass xmlClass : classes) {350        setClassInvocationFailure(xmlClass.getSupportClass(), instance);351      }352    }353    String[] beforeGroups= annotation.getBeforeGroups();354    if(null != beforeGroups && beforeGroups.length > 0) {355      for(String group: beforeGroups) {356        m_beforegroupsFailures.put(group, Boolean.FALSE);357      }358    }359  }360  /**361   * @return true if this class or a parent class failed to initialize.362   */363  private boolean classConfigurationFailed(Class<?> cls) {364    synchronized(m_classInvocationResults){365       for (Class<?> c : m_classInvocationResults.keySet()) {366         if (c == cls || c.isAssignableFrom(cls)) {367           return true;368         }369       }370       return false;371    }372  }373  /**374   * @return true if this class has successfully run all its @Configuration375   * method or false if at least one of these methods failed.376   */377  private boolean confInvocationPassed(ITestNGMethod method, ITestNGMethod currentTestMethod,378      IClass testClass, Object instance) {379    boolean result = true;380    Class<?> cls = testClass.getRealClass();381    if(m_suiteState.isFailed()) {382      result = false;383    } else {384      if (classConfigurationFailed(cls)) {385        if (! m_continueOnFailedConfiguration) {386          result = !classConfigurationFailed(cls);387        } else {388          synchronized(m_classInvocationResults){389             result = !m_classInvocationResults.get(cls).contains(instance);390          }391        }392      }393      // if method is BeforeClass, currentTestMethod will be null394      else if (m_continueOnFailedConfiguration &&395              currentTestMethod != null &&396              m_methodInvocationResults.containsKey(currentTestMethod)) {397        result = !m_methodInvocationResults.get(currentTestMethod).contains(getMethodInvocationToken(currentTestMethod, instance));398      }399      else if (! m_continueOnFailedConfiguration) {400        synchronized(m_classInvocationResults){401           for(Class<?> clazz : m_classInvocationResults.keySet()) {402             if (clazz.isAssignableFrom(cls)) {403               result = false;404               break;405             }406           }407        }408      }409    }410    // check if there are failed @BeforeGroups411    String[] groups = method.getGroups();412    if(null != groups && groups.length > 0) {413      for(String group : groups) {414        if(m_beforegroupsFailures.containsKey(group)) {415          result = false;416          break;417        }418      }419    }420    return result;421  }422   // Creates a token for tracking a unique invocation of a method on an instance.423   // Is used when configFailurePolicy=continue.424  private static Object getMethodInvocationToken(ITestNGMethod method, Object instance) {425    return String.format("%s+%d+%d", instance.toString(), method.getCurrentInvocationCount(), method.getParameterInvocationCount());426  }427  /**428   * Effectively invokes a configuration method on all passed in instances.429   * TODO: Should change this method to be more like invokeMethod() so that we can430   * handle calls to {@code IInvokedMethodListener} better.431   *432   * @param targetInstance the instance to invoke the configuration method on433   * @param tm the configuration method434   * @param params the parameters needed for method invocation435   * @param testResult436   * @throws InvocationTargetException437   * @throws IllegalAccessException438   */439  private void invokeConfigurationMethod(Object targetInstance,440                                         ITestNGMethod tm,441                                         Object[] params,442                                         ITestResult testResult)443    throws InvocationTargetException, IllegalAccessException444  {445    // Mark this method with the current thread id446    tm.setId(ThreadUtil.currentThreadInfo());447    {448      InvokedMethod invokedMethod= new InvokedMethod(targetInstance,449                                          tm,450                                          System.currentTimeMillis(),451                                          testResult);452      runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);453      m_notifier.addInvokedMethod(invokedMethod);454      try {455        Reporter.setCurrentTestResult(testResult);456        ConstructorOrMethod method = tm.getConstructorOrMethod();457        //458        // If this method is a IConfigurable, invoke its run() method459        //460        IConfigurable configurableInstance =461          IConfigurable.class.isAssignableFrom(method.getDeclaringClass()) ?462          (IConfigurable) targetInstance : m_configuration.getConfigurable();463        if (configurableInstance != null) {464          MethodInvocationHelper.invokeConfigurable(targetInstance, params, configurableInstance, method.getMethod(),465              testResult);466        }467        else {468          //469          // Not a IConfigurable, invoke directly470          //471          if (MethodHelper.calculateTimeOut(tm) <= 0) {472            MethodInvocationHelper.invokeMethod(method.getMethod(), targetInstance, params);473          }474          else {475            MethodInvocationHelper.invokeWithTimeout(tm, targetInstance, params, testResult);476            if (!testResult.isSuccess()) {477              // A time out happened478              throwConfigurationFailure(testResult, testResult.getThrowable());479              throw testResult.getThrowable();480            }481          }482        }483      }484      catch (InvocationTargetException | IllegalAccessException ex) {485       throwConfigurationFailure(testResult, ex);486       throw ex;487      } catch (Throwable ex) {488        throwConfigurationFailure(testResult, ex);489        throw new TestNGException(ex);490      }491      finally {492        testResult.setEndMillis(System.currentTimeMillis());493        Reporter.setCurrentTestResult(testResult);494        runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);495        Reporter.setCurrentTestResult(null);496      }497    }498  }499  private void throwConfigurationFailure(ITestResult testResult, Throwable ex)500  {501    testResult.setStatus(ITestResult.FAILURE);502    testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());503  }504  private void runInvokedMethodListeners(InvokedMethodListenerMethod listenerMethod, IInvokedMethod invokedMethod,505      ITestResult testResult)506  {507    if ( noListenersPresent() ) {508      return;509    }510    InvokedMethodListenerInvoker invoker = new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);511    for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {512      invoker.invokeListener(currentListener, invokedMethod);513    }514  }515  private boolean noListenersPresent() {516    return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.size() == 0);517  }518  // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.519  private ITestResult invokeMethod(Object instance,520                                   final ITestNGMethod tm,521                                   Object[] parameterValues,522                                   int parametersIndex,523                                   XmlSuite suite,524                                   Map<String, String> params,525                                   ITestClass testClass,526                                   ITestNGMethod[] beforeMethods,527                                   ITestNGMethod[] afterMethods,528                                   ConfigurationGroupMethods groupMethods,529                                   FailureContext failureContext) {530    TestResult testResult = new TestResult();531    //532    // Invoke beforeGroups configurations533    //534    invokeBeforeGroupsConfigurations(testClass, tm, groupMethods, suite, params,535        instance);536    //537    // Invoke beforeMethods only if538    // - firstTimeOnly is not set539    // - firstTimeOnly is set, and we are reaching at the first invocationCount540    //541    invokeConfigurations(testClass, tm,542      filterConfigurationMethods(tm, beforeMethods, true /* beforeMethods */),543      suite, params, parameterValues,544      instance, testResult);545    InvokedMethod invokedMethod = new InvokedMethod(instance,546        tm,547        System.currentTimeMillis(),548        testResult);549    if (!confInvocationPassed(tm, tm, testClass, instance)) {550      ITestResult result = registerSkippedTestResult(tm, instance, System.currentTimeMillis(),551              getExceptionDetails(instance));552      m_notifier.addSkippedTest(tm, result);553      tm.incrementCurrentInvocationCount();554      runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);555      invokeConfigurations(testClass, tm,556          filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),557          suite, params, parameterValues,558          instance,559          testResult);560      invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite, params, instance);561      return result;562    }563    //564    // Create the ExtraOutput for this method565    //566    try {567      testResult.init(testClass, instance,568                                 tm,569                                 null,570                                 System.currentTimeMillis(),571                                 0,572                                 m_testContext);573      testResult.setParameters(parameterValues);574      testResult.setParameterIndex(parametersIndex);575      testResult.setHost(m_testContext.getHost());576      testResult.setStatus(ITestResult.STARTED);577      Reporter.setCurrentTestResult(testResult);578      // Fix from ansgarkonermann579      // invokedMethod is used in the finally, which can be invoked if580      // any of the test listeners throws an exception, therefore,581      // invokedMethod must have a value before we get here582      if (!m_suiteState.isFailed()) {583        runTestListeners(testResult);584      }585      log(3, "Invoking " + tm.getQualifiedName());586      runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);587      m_notifier.addInvokedMethod(invokedMethod);588      Method thisMethod = tm.getConstructorOrMethod().getMethod();589      // If this method is a IHookable, invoke its run() method590      IHookable hookableInstance =591          IHookable.class.isAssignableFrom(tm.getRealClass()) ?592          (IHookable) instance : m_configuration.getHookable();593      if (MethodHelper.calculateTimeOut(tm) <= 0) {594        if (hookableInstance != null) {595          MethodInvocationHelper.invokeHookable(instance,596              parameterValues, hookableInstance, thisMethod, testResult);597        } else {598          // Not a IHookable, invoke directly599          MethodInvocationHelper.invokeMethod(thisMethod, instance,600              parameterValues);601        }602        setTestStatus(testResult, ITestResult.SUCCESS);603      } else {604        // Method with a timeout605        MethodInvocationHelper.invokeWithTimeout(tm, instance, parameterValues, testResult, hookableInstance);606      }607    }608    catch(InvocationTargetException ite) {609      testResult.setThrowable(ite.getCause());610      setTestStatus(testResult, ITestResult.FAILURE);611    }612    catch(ThreadExecutionException tee) { // wrapper for TestNGRuntimeException613      Throwable cause= tee.getCause();614      if(TestNGRuntimeException.class.equals(cause.getClass())) {615        testResult.setThrowable(cause.getCause());616      }617      else {618        testResult.setThrowable(cause);619      }620      setTestStatus(testResult, ITestResult.FAILURE);621    }622    catch(Throwable thr) { // covers the non-wrapper exceptions623      testResult.setThrowable(thr);624      setTestStatus(testResult, ITestResult.FAILURE);625    }626    finally {627      // Set end time ASAP628      testResult.setEndMillis(System.currentTimeMillis());629      ExpectedExceptionsHolder expectedExceptionClasses630          = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));631      List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);632      handleInvocationResults(tm, results, expectedExceptionClasses, failureContext);633      // If this method has a data provider and just failed, memorize the number634      // at which it failed.635      // Note: we're not exactly testing that this method has a data provider, just636      // that it has parameters, so might have to revisit this if bugs get reported637      // for the case where this method has parameters that don't come from a data638      // provider639      if (testResult.getThrowable() != null && parameterValues.length > 0) {640        tm.addFailedInvocationNumber(parametersIndex);641      }642      //643      // Increment the invocation count for this method644      //645      tm.incrementCurrentInvocationCount();646      // Run invokedMethodListeners after updating TestResult647      runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);648      runTestListeners(testResult);649      collectResults(tm, testResult);650      //651      // Invoke afterMethods only if652      // - lastTimeOnly is not set653      // - lastTimeOnly is set, and we are reaching the last invocationCount654      //655      invokeConfigurations(testClass, tm,656          filterConfigurationMethods(tm, afterMethods, false /* beforeMethods */),657          suite, params, parameterValues,658          instance,659          testResult);660      //661      // Invoke afterGroups configurations662      //663      invokeAfterGroupsConfigurations(testClass, tm, groupMethods, suite,664          params, instance);665      // Reset the test result last. If we do this too early, Reporter.log()666      // invocations from listeners will be discarded667      Reporter.setCurrentTestResult(null);668    }669    return testResult;670  }671  private static void setTestStatus(ITestResult result, int status) {672    // set the test to success as long as the testResult hasn't been changed by the user via673    // Reporter.getCurrentTestResult674    if (result.getStatus() == ITestResult.STARTED) {675      result.setStatus(status);676    }677  }678  private Throwable getExceptionDetails(Object instance) {679    Set<ITestResult> configResults = m_testContext.getFailedConfigurations().getAllResults();680    if (configResults.isEmpty()) {681      configResults = m_testContext.getSkippedConfigurations().getAllResults();682    }683    for (ITestResult configResult : configResults) {684      if (sameInstance(configResult, instance)) {685        return configResult.getThrowable();686      }687    }688    if (configResults.isEmpty()) {689      //if we are here it means we have a test method skip due to a @BeforeSuite failure in a different <test> maybe690      //So we will have to find out that first failure/skip and get its throwable and pack that information into our691      //current test method's test result.692      return getConfigFailureException();693    } else {694      //If we are here it perhaps means that the test method is being skipped because there was a configuration695      //failure in a different class due to @BeforeGroups being used.696      //So lets just find the first exception information and then just pack it in.697      return configResults.iterator().next().getThrowable();698    }699  }700  private Throwable getConfigFailureException() {701    Throwable t = null;702    for (IInvokedMethod method : m_testContext.getSuite().getAllInvokedMethods()) {703      ITestNGMethod m = method.getTestMethod();704      if (m.isBeforeSuiteConfiguration() && (! method.getTestResult().isSuccess())) {705        t = method.getTestResult().getThrowable();706        break;707      }708    }709    return t;710  }711  private boolean sameInstance(ITestResult configResult, Object instance) {712    return (configResult.getInstance()!= null ) && (configResult.getInstance().equals(instance));713  }714  void collectResults(ITestNGMethod testMethod, ITestResult result) {715      // Collect the results716      final int status = result.getStatus();717      if(ITestResult.SUCCESS == status) {718        m_notifier.addPassedTest(testMethod, result);719      }720      else if(ITestResult.SKIP == status) {721        m_notifier.addSkippedTest(testMethod, result);722      }723      else if(ITestResult.FAILURE == status) {724        m_notifier.addFailedTest(testMethod, result);725      }726      else if(ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {727        m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);728      }729      else {730        assert false : "UNKNOWN STATUS:" + status;731      }732  }733  /**734   * The array of methods contains @BeforeMethods if isBefore if true, @AfterMethods735   * otherwise.  This function removes all the methods that should not be run at this736   * point because they are either firstTimeOnly or lastTimeOnly and we haven't reached737   * the current invocationCount yet738   */739  private ITestNGMethod[] filterConfigurationMethods(ITestNGMethod tm,740      ITestNGMethod[] methods, boolean isBefore)741  {742    List<ITestNGMethod> result = Lists.newArrayList();743    for (ITestNGMethod m : methods) {744      ConfigurationMethod cm = (ConfigurationMethod) m;745      if (isBefore) {746        if (! cm.isFirstTimeOnly() ||747            (cm.isFirstTimeOnly() && tm.getCurrentInvocationCount() == 0))748        {749          result.add(m);750        }751      }752      else {753        if (! cm.isLastTimeOnly() || (cm.isLastTimeOnly() && !tm.hasMoreInvocation())) {754          result.add(m);755        }756      }757    }758    return result.toArray(new ITestNGMethod[result.size()]);759  }760  /**761   * invokeTestMethods() eventually converge here to invoke a single @Test method.762   * <p/>763   * This method is responsible for actually invoking the method. It decides if the invocation764   * must be done:765   * <ul>766   * <li>through an <code>IHookable</code></li>767   * <li>directly (through reflection)</li>768   * <li>in a separate thread (in case it needs to timeout)769   * </ul>770   *771   * <p/>772   * This method is also responsible for invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup773   * if it is the case for the passed in @Test method.774   */775  protected ITestResult invokeTestMethod(Object instance,776                                             final ITestNGMethod tm,777                                             Object[] parameterValues,778                                             int parametersIndex,779                                             XmlSuite suite,780                                             Map<String, String> params,781                                             ITestClass testClass,782                                             ITestNGMethod[] beforeMethods,783                                             ITestNGMethod[] afterMethods,784                                             ConfigurationGroupMethods groupMethods,785                                             FailureContext failureContext)786  {787    // Mark this method with the current thread id788    tm.setId(ThreadUtil.currentThreadInfo());789    ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,790                                      testClass, beforeMethods, afterMethods, groupMethods,791                                      failureContext);792    return result;793  }794  /**795   * Filter all the beforeGroups methods and invoke only those that apply796   * to the current test method797   */798  private void invokeBeforeGroupsConfigurations(ITestClass testClass,799                                                ITestNGMethod currentTestMethod,800                                                ConfigurationGroupMethods groupMethods,801                                                XmlSuite suite,802                                                Map<String, String> params,803                                                Object instance)804  {805    synchronized(groupMethods) {806      List<ITestNGMethod> filteredMethods = Lists.newArrayList();807      String[] groups = currentTestMethod.getGroups();808      Map<String, List<ITestNGMethod>> beforeGroupMap = groupMethods.getBeforeGroupsMap();809      for (String group : groups) {810        List<ITestNGMethod> methods = beforeGroupMap.get(group);811        if (methods != null) {812          filteredMethods.addAll(methods);813        }814      }815      ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[filteredMethods.size()]);816      //817      // Invoke the right groups methods818      //819      if(beforeMethodsArray.length > 0) {820        // don't pass the IClass or the instance as the method may be external821        // the invocation must be similar to @BeforeTest/@BeforeSuite822        invokeConfigurations(null, beforeMethodsArray, suite, params,823            /* no parameter values */ null, instance);824      }825      //826      // Remove them so they don't get run again827      //828      groupMethods.removeBeforeGroups(groups);829    }830  }831  private void invokeAfterGroupsConfigurations(ITestClass testClass,832                                               ITestNGMethod currentTestMethod,833                                               ConfigurationGroupMethods groupMethods,834                                               XmlSuite suite,835                                               Map<String, String> params,836                                               Object instance)837  {838    // Skip this if the current method doesn't belong to any group839    // (only a method that belongs to a group can trigger the invocation840    // of afterGroups methods)841    if (currentTestMethod.getGroups().length == 0) {842      return;843    }844    // See if the currentMethod is the last method in any of the groups845    // it belongs to846    Map<String, String> filteredGroups = Maps.newHashMap();847    String[] groups = currentTestMethod.getGroups();848    synchronized(groupMethods) {849      for (String group : groups) {850        if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {851          filteredGroups.put(group, group);852        }853      }854      if(filteredGroups.isEmpty()) {855        return;856      }857      // The list of afterMethods to run858      Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();859      // Now filteredGroups contains all the groups for which we need to run the afterGroups860      // method.  Find all the methods that correspond to these groups and invoke them.861      Map<String, List<ITestNGMethod>> map = groupMethods.getAfterGroupsMap();862      for (String g : filteredGroups.values()) {863        List<ITestNGMethod> methods = map.get(g);864        // Note:  should put them in a map if we want to make sure the same afterGroups865        // doesn't get run twice866        if (methods != null) {867          for (ITestNGMethod m : methods) {868            afterMethods.put(m, m);869          }870        }871      }872      // Got our afterMethods, invoke them873      ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[afterMethods.size()]);874      // don't pass the IClass or the instance as the method may be external875      // the invocation must be similar to @BeforeTest/@BeforeSuite876      invokeConfigurations(null, afterMethodsArray, suite, params,877          /* no parameter values */ null, instance);878      // Remove the groups so they don't get run again879      groupMethods.removeAfterGroups(filteredGroups.keySet());880    }881  }882  private Object[] getParametersFromIndex(Iterator<Object[]> parametersValues, int index) {883    while (parametersValues.hasNext()) {884      Object[] parameters = parametersValues.next();885      if (index == 0) {886        return parameters;887      }888      index--;889    }890    return null;891  }892  int retryFailed(Object instance,893                           final ITestNGMethod tm,894                           XmlSuite suite,895                           ITestClass testClass,896                           ITestNGMethod[] beforeMethods,897                           ITestNGMethod[] afterMethods,898                           ConfigurationGroupMethods groupMethods,899                           List<ITestResult> result,900                           int failureCount,901                           ExpectedExceptionsHolder expectedExceptionHolder,902                           ITestContext testContext,903                           Map<String, String> parameters,904                           int parametersIndex) {905    final FailureContext failure = new FailureContext();906    failure.count = failureCount;907    do {908      failure.instances = Lists.newArrayList ();909      Map<String, String> allParameters = Maps.newHashMap();910      /**911       * TODO: This recreates all the parameters every time when we only need912       * one specific set. Should optimize it by only recreating the set needed.913       */914      ParameterBag bag = createParameters(tm, parameters,915          allParameters, suite, testContext, null /* fedInstance */);916      Object[] parameterValues =917          getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);918      result.add(invokeMethod(instance, tm, parameterValues, parametersIndex, suite,919          allParameters, testClass, beforeMethods, afterMethods, groupMethods, failure));920    }921    while (!failure.instances.isEmpty());922    return failure.count;923  }924  private ParameterBag createParameters(ITestNGMethod testMethod,925                                        Map<String, String> parameters,926                                        Map<String, String> allParameterNames,927                                        XmlSuite suite,928                                        ITestContext testContext,929                                        Object fedInstance)930  {931    Object instance;932    if (fedInstance != null) {933      instance = fedInstance;934    }935    else {936      instance = testMethod.getInstance();937    }938    ParameterBag bag = handleParameters(testMethod,939        instance, allParameterNames, parameters, null, suite, testContext, fedInstance, null);940    return bag;941  }942  /**943   * Invoke all the test methods. Note the plural: the method passed in944   * parameter might be invoked several times if the test class it belongs945   * to has more than one instance (i.e., if an @Factory method has been946   * declared somewhere that returns several instances of this TestClass).947   * If no @Factory method was specified, testMethod will only be invoked948   * once.949   * <p/>950   * Note that this method also takes care of invoking the beforeTestMethod951   * and afterTestMethod, if any.952   *953   * Note (alex): this method can be refactored to use a SingleTestMethodWorker that954   * directly invokes955   * {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map, ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)}956   * and this would simplify the implementation (see how DataTestMethodWorker is used)957   */958  @Override959  public List<ITestResult> invokeTestMethods(ITestNGMethod testMethod,960                                             XmlSuite suite,961                                             Map<String, String> testParameters,962                                             ConfigurationGroupMethods groupMethods,963                                             Object instance,964                                             ITestContext testContext)965  {966    // Potential bug here if the test method was declared on a parent class967    assert null != testMethod.getTestClass()968        : "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass();969    if (!MethodHelper.isEnabled(testMethod.getConstructorOrMethod().getMethod(), m_annotationFinder)) {970      // return if the method is not enabled. No need to do any more calculations971      return Collections.emptyList();972    }973    // By the time this testMethod to be invoked,974    // all dependencies should be already run or we need to skip this method,975    // so invocation count should not affect dependencies check976    final String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());977    if (okToProceed != null) {978      //979      // Not okToProceed. Test is being skipped980      //981      ITestResult result = registerSkippedTestResult(testMethod, null, System.currentTimeMillis(),982          new Throwable(okToProceed));983      m_notifier.addSkippedTest(testMethod, result);984      return Collections.singletonList(result);985    }986    final Map<String, String> parameters =987        testMethod.findMethodParameters(testContext.getCurrentXmlTest());988    // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.989    if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {990      return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);991    }992    long timeOutInvocationCount = testMethod.getInvocationTimeOut();993    //FIXME: Is this correct?994    boolean onlyOne = testMethod.getThreadPoolSize() > 1 ||995      timeOutInvocationCount > 0;996    int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();997    ExpectedExceptionsHolder expectedExceptionHolder =998        new ExpectedExceptionsHolder(m_annotationFinder, testMethod,999                                     new RegexpExpectedExceptionsHolder(m_annotationFinder, testMethod));1000    final ITestClass testClass= testMethod.getTestClass();1001    final List<ITestResult> result = Lists.newArrayList();1002    final FailureContext failure = new FailureContext();1003    final ITestNGMethod[] beforeMethods = filterMethods(testClass, testClass.getBeforeTestMethods(), CAN_RUN_FROM_CLASS);1004    final ITestNGMethod[] afterMethods = filterMethods(testClass, testClass.getAfterTestMethods(), CAN_RUN_FROM_CLASS);1005    while(invocationCount-- > 0) {1006      if(false) {1007        // Prevent code formatting1008      }1009      //1010      // No threads, regular invocation1011      //1012      else {1013        // Used in catch statement1014        long start = System.currentTimeMillis();1015        Map<String, String> allParameterNames = Maps.newHashMap();1016        ParameterBag bag = createParameters(testMethod,1017            parameters, allParameterNames, suite, testContext, instance);1018        if (bag.hasErrors()) {1019          final ITestResult tr = bag.errorResult;1020          Throwable throwable = tr.getThrowable();1021          if (throwable instanceof TestNGException) {1022            tr.setStatus(ITestResult.FAILURE);1023            m_notifier.addFailedTest(testMethod, tr);1024          } else {1025            tr.setStatus(ITestResult.SKIP);1026            m_notifier.addSkippedTest(testMethod, tr);1027          }1028          runTestListeners(tr);1029          result.add(tr);1030          continue;1031        }1032        Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;1033        int parametersIndex = 0;1034        try {1035          if (bag.parameterHolder.origin == ParameterOrigin.ORIGIN_DATA_PROVIDER &&1036              bag.parameterHolder.dataProviderHolder.annotation.isParallel()) {1037            List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();1038            while (allParameterValues.hasNext()) {1039              Object[] next = allParameterValues.next();1040              if (next == null) {1041                // skipped value1042                parametersIndex++;1043                continue;1044              }1045              Object[] parameterValues = injectParameters(next,1046                  testMethod.getConstructorOrMethod().getMethod(), testContext, null /* test result */);1047              TestMethodWithDataProviderMethodWorker w =1048                new TestMethodWithDataProviderMethodWorker(this,1049                    testMethod, parametersIndex,1050                    parameterValues, instance, suite, parameters, testClass,1051                    beforeMethods, afterMethods, groupMethods,1052                    expectedExceptionHolder, testContext, m_skipFailedInvocationCounts,1053                    invocationCount, failure.count, m_notifier);1054              workers.add(w);1055              // testng387: increment the param index in the bag.1056              parametersIndex++;1057            }1058            PoolService<List<ITestResult>> ps =1059                    new PoolService<>(suite.getDataProviderThreadCount());1060            List<List<ITestResult>> r = ps.submitTasksAndWait(workers);1061            for (List<ITestResult> l2 : r) {1062              result.addAll(l2);1063            }1064          } else {1065            while (allParameterValues.hasNext()) {1066              Object[] next = allParameterValues.next();1067              if (next == null) {1068                // skipped value1069                parametersIndex++;1070                continue;1071              }1072              Object[] parameterValues = injectParameters(next,1073                  testMethod.getConstructorOrMethod().getMethod(), testContext, null /* test result */);1074              List<ITestResult> tmpResults = Lists.newArrayList();1075              int tmpResultsIndex = -1;1076              try {1077                tmpResults.add(invokeTestMethod(instance,1078                    testMethod,1079                    parameterValues,1080                    parametersIndex,1081                    suite,1082                    parameters,1083                    testClass,1084                    beforeMethods,1085                    afterMethods,1086                    groupMethods, failure));1087                tmpResultsIndex++;1088              }1089              finally {1090              	boolean lastSucces = false;1091                if (tmpResultsIndex >= 0) {1092                  lastSucces = (tmpResults.get(tmpResultsIndex).getStatus() == ITestResult.SUCCESS);1093                }1094                if (failure.instances.isEmpty() || lastSucces) {1095                  result.addAll(tmpResults);1096                } else {1097                    List<ITestResult> retryResults = Lists.newArrayList();1098                    failure.count = retryFailed(1099                    		instance, testMethod, suite, testClass, beforeMethods,1100                     afterMethods, groupMethods, retryResults,1101                     failure.count, expectedExceptionHolder,1102                     testContext, parameters, parametersIndex);1103                  result.addAll(retryResults);1104                }1105                //1106                // If we have a failure, skip all the1107                // other invocationCounts1108                //1109                if (failure.count > 01110                      && (m_skipFailedInvocationCounts1111                            || testMethod.skipFailedInvocations())) {1112                  while (invocationCount-- > 0) {1113                    result.add(registerSkippedTestResult(testMethod, instance, System.currentTimeMillis(), null));1114                  }1115                }1116              }// end finally1117              parametersIndex++;1118            }1119          }1120        }1121        catch (Throwable cause) {1122          ITestResult r =1123              new TestResult(testMethod.getTestClass(),1124                instance,1125                testMethod,1126                cause,1127                start,1128                System.currentTimeMillis(),1129                m_testContext);1130            r.setStatus(TestResult.FAILURE);1131            result.add(r);1132            runTestListeners(r);1133            m_notifier.addFailedTest(testMethod, r);1134        } // catch1135      }1136    }1137    return result;1138  } // invokeTestMethod1139  private ITestResult registerSkippedTestResult(ITestNGMethod testMethod, Object instance,1140      long start, Throwable throwable) {1141    ITestResult result =1142      new TestResult(testMethod.getTestClass(),1143        instance,1144        testMethod,1145        throwable,1146        start,1147        System.currentTimeMillis(),1148        m_testContext);1149    result.setStatus(TestResult.SKIP);1150    Reporter.setCurrentTestResult(result);1151    runTestListeners(result);1152    return result;1153  }1154  /**1155   * Gets an array of parameter values returned by data provider or the ones that1156   * are injected based on parameter type. The method also checks for {@code NoInjection}1157   * annotation1158   *1159   * @param parameterValues parameter values from a data provider1160   * @param method method to be invoked1161   * @param context test context1162   * @param testResult test result1163   */1164  private Object[] injectParameters(Object[] parameterValues, Method method,1165      ITestContext context, ITestResult testResult)1166    throws TestNGException {1167    final MethodMatcher matcher = new DataProviderMethodMatcher(1168      new MethodMatcherContext(method, parameterValues, context, testResult)1169    );1170    return matcher.getConformingArguments();1171  }1172  private ParameterBag handleParameters(ITestNGMethod testMethod,1173      Object instance,1174      Map<String, String> allParameterNames,1175      Map<String, String> parameters,1176      Object[] parameterValues,1177      XmlSuite suite,1178      ITestContext testContext,1179      Object fedInstance,1180      ITestResult testResult)1181  {1182    try {1183      return new ParameterBag(1184          Parameters.handleParameters(testMethod,1185            allParameterNames,1186            instance,1187            new Parameters.MethodParameters(parameters,1188                testMethod.findMethodParameters(testContext.getCurrentXmlTest()),1189                parameterValues,1190                testMethod.getConstructorOrMethod().getMethod(), testContext, testResult),1191            suite,1192            m_annotationFinder,1193            fedInstance));1194    }1195    catch(Throwable cause) {1196      String msg = Utils.longStackTrace(cause.getCause() != null ? cause.getCause() : cause, true);1197      if (Strings.isNotNullAndNotEmpty(msg) ) {1198        Utils.error(msg);1199      }1200      return new ParameterBag(1201          new TestResult(1202              testMethod.getTestClass(),1203              instance,1204              testMethod,...Source:BaseUnitTest.java  
...206        }207        ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class);208        when(constructorOrMethod.getMethod()).thenReturn(method);209        ITestNGMethod iTestNGMethod = mock(ITestNGMethod.class);210        when(iTestNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);211        when(iTestNGMethod.getMethodName()).thenReturn(methodName);212        when(iTestNGMethod.getRealClass()).thenReturn(clazz);213        when(iTestNGMethod.isTest()).thenReturn(true);214        when(iTestNGMethod.getInvocationCount()).thenReturn(1);215        return iTestNGMethod;216    }217    @SuppressWarnings("unchecked")218    protected static ITestClass getMockITestClass(Class aClass) {219        ITestClass iTestClass = mock(ITestClass.class);220        when(iTestClass.getRealClass()).thenReturn(aClass);221        return iTestClass;222    }223    protected static ITestResult getMockITestResult(Integer status) {224        Method method;225        try {226            method = TestNGTestClassWithSuite.class.getMethod("iTestResultMethodWithDetails");227        } catch (NoSuchMethodException e) {228            throw new RuntimeException(e);229        }230        ConstructorOrMethod constructorOrMethod = mock(ConstructorOrMethod.class);231        when(constructorOrMethod.getMethod()).thenReturn(method);232        ITestNGMethod iTestNGMethod = mock(ITestNGMethod.class);233        when(iTestNGMethod.getConstructorOrMethod()).thenReturn(constructorOrMethod);234        ITestResult iTestResult = mock(ITestResult.class);235        when(iTestResult.getStatus()).thenReturn(status);236        when(iTestResult.getMethod()).thenReturn(iTestNGMethod);237        return iTestResult;238    }239    protected BuggyExecutionListener getBuggyExecutionListener() {240        return getBuggyExecutionListener(true);241    }242    @SuppressWarnings("SameParameterValue")243    protected BuggyExecutionListener getBuggyExecutionListener(boolean withOverrideCopyFile) {244        return new BuggyExecutionListener(TEST_LOGGER, TEST_LOGGER, TEST_LOGGER) {245            @Override246            public void copyFile(File sourceFile, File destFile) throws IOException {247                if (!withOverrideCopyFile) {...Source:ITestNGMethod.java  
...3637  /**38   * @return the corresponding Java test method.39   * @deprecated This method is deprecated and can now return null. Use40   * getConstructorOrMethod() instead.41   */42  @Deprecated43  Method getMethod();4445  /**46   * Returns the method name. This is needed for serialization because47   * methods are not Serializable.48   * @return the method name.49   */50  String getMethodName();5152  /**53   * @return All the instances the methods will be invoked upon.54   * This will typically be an array of one object in the absence55   * of an @Factory annotation.56   *57   * @deprecated Use getInstance().58   */59  @Deprecated60  Object[] getInstances();6162  Object getInstance();6364  /**65   * Needed for serialization.66   */67  long[] getInstanceHashCodes();6869  /**70   * @return The groups this method belongs to, possibly added to the groups71   * declared on the class.72   */73  String[] getGroups();7475  /**76   * @return The groups this method depends on, possibly added to the groups77   * declared on the class.78   */79  String[] getGroupsDependedUpon();8081  /**82   * If a group was not found.83   */84  String getMissingGroup();85  public void setMissingGroup(String group);8687  /**88   * Before and After groups89   */90  public String[] getBeforeGroups();91  public String[] getAfterGroups();9293  /**94   * @return The methods  this method depends on, possibly added to the methods95   * declared on the class.96   */97  String[] getMethodsDependedUpon();98  void addMethodDependedUpon(String methodName);99100  /**101   * @return true if this method was annotated with @Test102   */103  boolean isTest();104105  /**106   * @return true if this method was annotated with @Configuration107   * and beforeTestMethod = true108   */109  boolean isBeforeMethodConfiguration();110111  /**112   * @return true if this method was annotated with @Configuration113   * and beforeTestMethod = false114   */115  boolean isAfterMethodConfiguration();116117  /**118   * @return true if this method was annotated with @Configuration119   * and beforeClassMethod = true120   */121  boolean isBeforeClassConfiguration();122123  /**124   * @return true if this method was annotated with @Configuration125   * and beforeClassMethod = false126   */127  boolean isAfterClassConfiguration();128129  /**130   * @return true if this method was annotated with @Configuration131   * and beforeSuite = true132   */133  boolean isBeforeSuiteConfiguration();134135  /**136   * @return true if this method was annotated with @Configuration137   * and afterSuite = true138   */139  boolean isAfterSuiteConfiguration();140141  /**142   * @return <tt>true</tt> if this method is a @BeforeTest (@Configuration beforeTest=true)143   */144  boolean isBeforeTestConfiguration();145146  /**147   * @return <tt>true</tt> if this method is an @AfterTest (@Configuration afterTest=true)148   */149  boolean isAfterTestConfiguration();150151  boolean isBeforeGroupsConfiguration();152153  boolean isAfterGroupsConfiguration();154155  /**156   * @return The timeout in milliseconds.157   */158  long getTimeOut();159  void setTimeOut(long timeOut);160161  /**162   * @return the number of times this method needs to be invoked.163   */164  int getInvocationCount();165  void setInvocationCount(int count);166167  /**168   * @return the total number of thimes this method needs to be invoked, including possible169   *         clones of this method - this is relevant when threadPoolSize is bigger than 1170   *         where each clone of this method is only invoked once individually, i.e.171   *         {@link org.testng.ITestNGMethod#getInvocationCount()} would always return 1.172   */173  int getTotalInvocationCount();174175  /**176   * @return the success percentage for this method (between 0 and 100).177   */178  int getSuccessPercentage();179180  /**181   * @return The id of the thread this method was run in.182   */183  String getId();184185  void setId(String id);186187  long getDate();188189  void setDate(long date);190191  /**192   * Returns if this ITestNGMethod can be invoked from within IClass.193   */194  boolean canRunFromClass(IClass testClass);195196  /**197   * @return true if this method is alwaysRun=true198   */199  boolean isAlwaysRun();200201  /**202   * @return the number of threads to be used when invoking the method on parallel203   */204  int getThreadPoolSize();205206  void setThreadPoolSize(int threadPoolSize);207208  boolean getEnabled();209210  public String getDescription();211  void setDescription(String description);212213  public void incrementCurrentInvocationCount();214  public int getCurrentInvocationCount();215  public void setParameterInvocationCount(int n);216  public int getParameterInvocationCount();217218  public ITestNGMethod clone();219220  public IRetryAnalyzer getRetryAnalyzer();221  public void setRetryAnalyzer(IRetryAnalyzer retryAnalyzer);222223  public boolean skipFailedInvocations();224  public void setSkipFailedInvocations(boolean skip);225226  /**227   * The time under which all invocationCount methods need to complete by.228   */229  public long getInvocationTimeOut();230231  public boolean ignoreMissingDependencies();232  public void setIgnoreMissingDependencies(boolean ignore);233234  /**235   * Which invocation numbers of this method should be used (only applicable236   * if it uses a data provider). If this value is an empty list, use all the values237   * returned from the data provider.  These values are read from the XML file in238   * the <include invocationNumbers="..."> tag.239   */240  public List<Integer> getInvocationNumbers();241  public void setInvocationNumbers(List<Integer> numbers);242243  /**244   * The list of invocation numbers that failed, which is only applicable for245   * methods that have a data provider.246   */247  public void addFailedInvocationNumber(int number);248  public List<Integer> getFailedInvocationNumbers();249250  /**251   * The scheduling priority. Lower priorities get scheduled first.252   */253  public int getPriority();254  public void setPriority(int priority);255256  /**257   * @return the XmlTest this method belongs to.258   */259  public XmlTest getXmlTest();260261  ConstructorOrMethod getConstructorOrMethod();262263  /**264   * @return the parameters found in the include tag, if any265   * @param test266   */267  Map<String, String> findMethodParameters(XmlTest test);268}
...Source:IAbstractTest.java  
...56        // do nothing57    }58    @DataProvider(name = "DataProvider", parallel = true)59    default Object[][] createData(final ITestNGMethod testMethod, ITestContext context) {60        Annotation[] annotations = testMethod.getConstructorOrMethod().getMethod().getDeclaredAnnotations();61        Object[][] objects = DataProviderFactory.getDataProvider(annotations, context, testMethod);62        return objects;63    }64    @DataProvider(name = "SingleDataProvider")65    default Object[][] createDataSingleThread(final ITestNGMethod testMethod,66            ITestContext context) {67        Annotation[] annotations = testMethod.getConstructorOrMethod().getMethod().getDeclaredAnnotations();68        Object[][] objects = DataProviderFactory.getDataProvider(annotations, context, testMethod);69        return objects;70    }71    /**72     * Pause for specified timeout.73     *74     * @param timeout in seconds.75     */76    default void pause(long timeout) {77        CommonUtils.pause(timeout);78    }79    default void pause(Double timeout) {80        CommonUtils.pause(timeout);81    }...Source:MyTestNGAnnotationListener.java  
...33        }34    }35    36    private boolean annotationPresent(IInvokedMethod method, Class clazz) {37        boolean retVal = method.getTestMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(clazz) ? true : false;38        return retVal;39    }40    41    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {42        if(method.isTestMethod()) {43            if(method.getClass().isAnnotationPresent(MyTestNGAnnotation.class)) {44                System.out.println("This gets invoked after every TestNG Test that has @MyTestNGAnnotation Annotation...");45            }46            if( !testSuccess ) {47                testResult.setStatus(ITestResult.FAILURE);48            }49        }50    }51    public void onTestStart(ITestResult result) {52        // TODO Auto-generated method stub53        54    }55    public void onTestSuccess(ITestResult result) {56        // TODO Auto-generated method stub57        58    }59    public void onTestFailure(ITestResult result) {60        // TODO Auto-generated method stub61        62    }63    public void onTestSkipped(ITestResult result) {64        // TODO Auto-generated method stub65        66    }67    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {68        // TODO Auto-generated method stub69        70    }71    public void onStart(ITestContext context) {try{72    //	Method m = context.getCurrentXmlTest().getPackages().get(0).getXmlClasses().get(1).getClass().getDeclaredMethod("tMe", null);73    	//System.out.println(context.getCurrentXmlTest().getPackages().get(0).getXmlClasses().get(1));74        for(ITestNGMethod m1 : context.getAllTestMethods()) {75            if(m1.getConstructorOrMethod().getMethod().isAnnotationPresent(MyTestNGAnnotation.class)) {76                //capture metadata information.77            	DataSet.name = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).name();78            	DataSet.city = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).city();79            	DataSet.state = m1.getConstructorOrMethod().getMethod().getAnnotation(MyTestNGAnnotation.class).state();80            }81           // m.invoke(new TestCalc(), null);82        } 83    }catch(Exception e){e.printStackTrace();} 84    }85    public void onFinish(ITestContext context) {86    	87    }88  89	public void transform(ITestAnnotation arg0, Class arg1, Constructor arg2,90			Method arg3) {91		92	}93}...Source:JUnitMethodFinder.java  
...49                /* allMethods[i].getDeclaringClass(), */ allMethod,50                m_annotationFinder,51                null,52                null); /* @@@ */53        ConstructorOrMethod method = m.getConstructorOrMethod();54        String methodName = method.getName();55        if (filter.accept(method) && !acceptedMethodNames.contains(methodName)) {56          vResult.add(m);57          acceptedMethodNames.add(methodName);58        }59      }60      current = current.getSuperclass();61    }62    return vResult.toArray(new ITestNGMethod[0]);63  }64  @Override65  public ITestNGMethod[] getBeforeTestMethods(Class cls) {66    return privateFindTestMethods(method -> "setUp".equals(method.getName()), cls);67  }...Source:TestngListener.java  
...108	 *            the im109	 * @return the case ID110	 */111	private String[] getCaseID(ITestNGMethod im) {112		Method m = im.getConstructorOrMethod().getMethod();113		CaseId caseId = m.getAnnotation(CaseId.class);114		if (null != caseId) {115			for (String str : caseId.id()) {116				System.out.println("++++++++++++++++>>>>>>>>>>>>>\n\n\n" + str + "<<<<<<<\n\n");117			}118			return caseId.id();119		}120		return null;121	}122	/*123	 * (non-Javadoc)124	 * 125	 * @see org.testng.TestListenerAdapter#getTestContexts()126	 */...Source:XrayListener.java  
...26     * @see org.testng.IInvokedMethodListener#beforeInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)27     */28    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {29        if(method.isTestMethod() && annotationPresent(method, Xray.class) ) {30            testResult.setAttribute("requirement", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).requirement());  31            testResult.setAttribute("test", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).test());32            testResult.setAttribute("labels", method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Xray.class).labels());33        }34    }35    36    private boolean annotationPresent(IInvokedMethod method, Class clazz) {37        boolean retVal = method.getTestMethod().getConstructorOrMethod().getMethod().isAnnotationPresent(clazz) ? true : false;38        return retVal;39    }40    41    /* (non-Javadoc)42     * @see org.testng.IInvokedMethodListener#afterInvocation(org.testng.IInvokedMethod, org.testng.ITestResult)43     */44    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {45        if(method.isTestMethod()) {46            if( !testSuccess ) {47                testResult.setStatus(ITestResult.FAILURE);48            }49        }50    }51    public void onTestStart(ITestResult result) {...getConstructorOrMethod
Using AI Code Generation
1org.testng.ITestNGMethod[] methods = new org.testng.ITestNGMethod[1];2methods[0] = org.testng.internal.MethodHelper.getConstructorOrMethod(testClass, method, null);3org.testng.internal.MethodHelper.setInvocationCount(methods[0], count);4methods[0].setInvocationCount(count);5org.testng.ITestNGMethod[] methods = new org.testng.ITestNGMethod[1];6methods[0] = org.testng.internal.MethodHelper.getConstructorOrMethod(testClass, method, null);7org.testng.internal.MethodHelper.setInvocationCount(methods[0], count);8methods[0].setInvocationCount(count);9org.testng.ITestNGMethod[] methods = new org.testng.ITestNGMethod[1];10methods[0] = org.testng.internal.MethodHelper.getConstructorOrMethod(testClass, method, null);11org.testng.internal.MethodHelper.setInvocationCount(methods[0], count);12methods[0].setInvocationCount(count);getConstructorOrMethod
Using AI Code Generation
1import org.testng.ITestNGMethod;2import org.testng.annotations.Test;3import org.testng.internal.ConstructorOrMethod;4public class TestNGExample {5	public void test() {6		TestNGExample testClass = new TestNGExample();7		ConstructorOrMethod constructorOrMethod = ITestNGMethod.getConstructorOrMethod(testClass.getClass());8		System.out.println("Constructor or method of the test class: "+constructorOrMethod);9	}10}11Constructor or method of the test class: public org.testng.internal.ConstructorOrMethod org.testng.ITestNGMethod.getConstructorOrMethod(java.lang.Class)TestNG 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!!
