Best Testng code snippet using org.testng.Interface ITestResult.setThrowable
Source:Invoker.java  
...267      Object instance,268      XmlSuite suite) {269    Throwable cause = ite.getCause() != null ? ite.getCause() : ite;270    if (isSkipExceptionAndSkip(cause)) {271      testResult.setThrowable(cause);272      handleConfigurationSkip(tm, testResult, annotation, currentTestMethod, instance, suite);273      return;274    }275    Utils.log(276        "",277        3,278        "Failed to invoke configuration method "279            + tm.getQualifiedName()280            + ":"281            + cause.getMessage());282    handleException(cause, tm, testResult, 1);283    testResult.setStatus(ITestResult.FAILURE);284    runConfigurationListeners(testResult, false /* after */);285    //286    // If in TestNG mode, need to take a look at the annotation to figure out287    // what kind of @Configuration method we're dealing with288    //289    if (null != annotation) {290      recordConfigurationInvocationFailed(291          tm, testResult.getTestClass(), annotation, currentTestMethod, instance, suite);292    }293  }294  /**295   * Record internally the failure of a Configuration, so that we can determine later if @Test296   * should be skipped.297   */298  private void recordConfigurationInvocationFailed(299      ITestNGMethod tm,300      IClass testClass,301      IConfigurationAnnotation annotation,302      ITestNGMethod currentTestMethod,303      Object instance,304      XmlSuite suite) {305    // If beforeTestClass or afterTestClass failed, mark either the config method's306    // entire class as failed, or the class under tests as failed, depending on307    // the configuration failure policy308    if (annotation.getBeforeTestClass() || annotation.getAfterTestClass()) {309      // tm is the configuration method, and currentTestMethod is null for BeforeClass310      // methods, so we need testClass311      if (m_continueOnFailedConfiguration) {312        setClassInvocationFailure(testClass.getRealClass(), instance);313      } else {314        setClassInvocationFailure(tm.getRealClass(), instance);315      }316    }317    // If before/afterTestMethod failed, mark either the config method's entire318    // class as failed, or just the current test method as failed, depending on319    // the configuration failure policy320    else if (annotation.getBeforeTestMethod() || annotation.getAfterTestMethod()) {321      if (m_continueOnFailedConfiguration) {322        setMethodInvocationFailure(currentTestMethod, instance);323      } else {324        setClassInvocationFailure(tm.getRealClass(), instance);325      }326    }327    // If beforeSuite or afterSuite failed, mark *all* the classes as failed328    // for configurations.  At this point, the entire Suite is screwed329    else if (annotation.getBeforeSuite() || annotation.getAfterSuite()) {330      m_suiteState.failed();331    }332    // beforeTest or afterTest:  mark all the classes in the same333    // <test> stanza as failed for configuration334    else if (annotation.getBeforeTest() || annotation.getAfterTest()) {335      setClassInvocationFailure(tm.getRealClass(), instance);336      XmlClass[] classes = ClassHelper.findClassesInSameTest(tm.getRealClass(), suite);337      for (XmlClass xmlClass : classes) {338        setClassInvocationFailure(xmlClass.getSupportClass(), instance);339      }340    }341    String[] beforeGroups = annotation.getBeforeGroups();342    for (String group : beforeGroups) {343      m_beforegroupsFailures.put(group, Boolean.FALSE);344    }345  }346  /** @return true if this class or a parent class failed to initialize. */347  private boolean classConfigurationFailed(Class<?> cls, Object instance) {348    return m_classInvocationResults349        .entrySet()350        .stream()351        .anyMatch(352            classSetEntry -> {353              Set<Object> obj = classSetEntry.getValue();354              Class<?> c = classSetEntry.getKey();355              boolean containsBeforeTestOrBeforeSuiteFailure = obj.contains(null);356              return c == cls357                  || c.isAssignableFrom(cls)358                      && (obj.contains(instance) || containsBeforeTestOrBeforeSuiteFailure);359            });360  }361  /**362   * @return true if this class has successfully run all its @Configuration method or false if at363   *     least one of these methods failed.364   */365  private boolean confInvocationPassed(366      ITestNGMethod method, ITestNGMethod currentTestMethod, IClass testClass, Object instance) {367    boolean result = true;368    Class<?> cls = testClass.getRealClass();369    if (m_suiteState.isFailed()) {370      result = false;371    } else {372      boolean hasConfigurationFailures = classConfigurationFailed(cls, instance);373      if (hasConfigurationFailures) {374        if (!m_continueOnFailedConfiguration) {375          result = false;376        } else {377          Set<Object> set = getInvocationResults(testClass);378          result = !set.contains(instance);379        }380        return result;381      }382      // if method is BeforeClass, currentTestMethod will be null383      if (m_continueOnFailedConfiguration && hasConfigFailure(currentTestMethod)) {384        Object key = TestNgMethodUtils.getMethodInvocationToken(currentTestMethod, instance);385        result = !m_methodInvocationResults.get(currentTestMethod).contains(key);386      } else if (!m_continueOnFailedConfiguration) {387        for (Class<?> clazz : m_classInvocationResults.keySet()) {388          if (clazz.isAssignableFrom(cls)389              && m_classInvocationResults.get(clazz).contains(instance)) {390            result = false;391            break;392          }393        }394      }395    }396    // check if there are failed @BeforeGroups397    String[] groups = method.getGroups();398    for (String group : groups) {399      if (m_beforegroupsFailures.containsKey(group)) {400        result = false;401        break;402      }403    }404    return result;405  }406  private boolean hasConfigFailure(ITestNGMethod currentTestMethod) {407    return currentTestMethod != null && m_methodInvocationResults.containsKey(currentTestMethod);408  }409  private Set<Object> getInvocationResults(IClass testClass) {410    Class<?> cls = testClass.getRealClass();411    Set<Object> set = null;412    // We need to continuously search till either our Set is not null (or) till we reached413    // Object class because it is very much possible that the test method is residing in a child414    // class415    // and maybe the parent method has configuration methods which may have had a failure416    // So lets walk up the inheritance tree until either we find failures or till we417    // reached the Object class.418    while (!cls.equals(Object.class)) {419      set = m_classInvocationResults.get(cls);420      if (set != null) {421        break;422      }423      cls = cls.getSuperclass();424    }425    if (set == null) {426      // This should never happen because we have walked up all the way till Object class427      // and yet found no failures, but our logic indicates that there was a failure somewhere up428      // the429      // inheritance order. We don't know what to do at this point.430      throw new IllegalStateException("No failure logs for " + testClass.getRealClass());431    }432    return set;433  }434  private static boolean isConfigMethodEligibleForScrutiny(ITestNGMethod tm) {435    if (!tm.isBeforeMethodConfiguration()) {436      return false;437    }438    if (!(tm instanceof ConfigurationMethod)) {439      return false;440    }441    ConfigurationMethod cfg = (ConfigurationMethod) tm;442    return cfg.isFirstTimeOnly();443  }444  /** Effectively invokes a configuration method on all passed in instances. */445  // TODO: Change this method to be more like invokeMethod() so that we can handle calls to {@code446  // IInvokedMethodListener} better.447  private void invokeConfigurationMethod(448      Object targetInstance, ITestNGMethod tm, Object[] params, ITestResult testResult)449      throws InvocationTargetException, IllegalAccessException {450    // Mark this method with the current thread id451    tm.setId(ThreadUtil.currentThreadInfo());452    InvokedMethod invokedMethod =453        new InvokedMethod(targetInstance, tm, System.currentTimeMillis(), testResult);454    runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);455    m_notifier.addInvokedMethod(invokedMethod);456    try {457      Reporter.setCurrentTestResult(testResult);458      ConstructorOrMethod method = tm.getConstructorOrMethod();459      IConfigurable configurableInstance = computeConfigurableInstance(method, targetInstance);460      if (RuntimeBehavior.isDryRun()) {461        testResult.setStatus(ITestResult.SUCCESS);462        return;463      }464      if (configurableInstance != null) {465        MethodInvocationHelper.invokeConfigurable(466            targetInstance, params, configurableInstance, method.getMethod(), testResult);467      } else {468        MethodInvocationHelper.invokeMethodConsideringTimeout(469            tm, method, targetInstance, params, testResult);470      }471      testResult.setStatus(ITestResult.SUCCESS);472    } catch (InvocationTargetException | IllegalAccessException ex) {473      throwConfigurationFailure(testResult, ex);474      testResult.setStatus(ITestResult.FAILURE);475      throw ex;476    } catch (Throwable ex) {477      throwConfigurationFailure(testResult, ex);478      testResult.setStatus(ITestResult.FAILURE);479      throw new TestNGException(ex);480    } finally {481      testResult.setEndMillis(System.currentTimeMillis());482      Reporter.setCurrentTestResult(testResult);483      runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);484      Reporter.setCurrentTestResult(null);485    }486  }487  private IConfigurable computeConfigurableInstance(488      ConstructorOrMethod method, Object targetInstance) {489    return IConfigurable.class.isAssignableFrom(method.getDeclaringClass())490        ? (IConfigurable) targetInstance491        : m_configuration.getConfigurable();492  }493  private void throwConfigurationFailure(ITestResult testResult, Throwable ex) {494    testResult.setStatus(ITestResult.FAILURE);495    testResult.setThrowable(ex.getCause() == null ? ex : ex.getCause());496  }497  private void runInvokedMethodListeners(498      InvokedMethodListenerMethod listenerMethod,499      IInvokedMethod invokedMethod,500      ITestResult testResult) {501    if (noListenersPresent()) {502      return;503    }504    InvokedMethodListenerInvoker invoker =505        new InvokedMethodListenerInvoker(listenerMethod, testResult, m_testContext);506    for (IInvokedMethodListener currentListener : m_invokedMethodListeners) {507      invoker.invokeListener(currentListener, invokedMethod);508    }509  }510  private boolean noListenersPresent() {511    return (m_invokedMethodListeners == null) || (m_invokedMethodListeners.isEmpty());512  }513  // pass both paramValues and paramIndex to be thread safe in case parallel=true + dataprovider.514  private ITestResult invokeMethod(515      Object instance,516      ITestNGMethod tm,517      Object[] parameterValues,518      int parametersIndex,519      XmlSuite suite,520      Map<String, String> params,521      ITestClass testClass,522      ITestNGMethod[] beforeMethods,523      ITestNGMethod[] afterMethods,524      ConfigurationGroupMethods groupMethods,525      FailureContext failureContext) {526    TestResult testResult = new TestResult();527    invokeBeforeGroupsConfigurations(tm, groupMethods, suite, params, instance);528    ITestNGMethod[] setupConfigMethods =529        TestNgMethodUtils.filterSetupConfigurationMethods(tm, beforeMethods);530    invokeConfigurations(531        testClass, tm, setupConfigMethods, suite, params, parameterValues, instance, testResult);532    InvokedMethod invokedMethod =533        new InvokedMethod(instance, tm, System.currentTimeMillis(), testResult);534    if (!confInvocationPassed(tm, tm, testClass, instance)) {535      Throwable exception = ExceptionUtils.getExceptionDetails(m_testContext, instance);536      ITestResult result = registerSkippedTestResult(tm, System.currentTimeMillis(), exception);537      copyAttributes(testResult, result);538      m_notifier.addSkippedTest(tm, result);539      tm.incrementCurrentInvocationCount();540      testResult.setMethod(tm);541      runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, result);542      runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, result);543      ITestNGMethod[] teardownConfigMethods =544          TestNgMethodUtils.filterTeardownConfigurationMethods(tm, afterMethods);545      invokeConfigurations(546          testClass,547          tm,548          teardownConfigMethods,549          suite,550          params,551          parameterValues,552          instance,553          testResult);554      invokeAfterGroupsConfigurations(tm, groupMethods, suite, params, instance);555      return result;556    }557    //558    // Create the ExtraOutput for this method559    //560    try {561      testResult.init(testClass, tm, null, System.currentTimeMillis(), 0, m_testContext);562      testResult.setParameters(parameterValues);563      testResult.setParameterIndex(parametersIndex);564      testResult.setHost(m_testContext.getHost());565      testResult.setStatus(ITestResult.STARTED);566      Reporter.setCurrentTestResult(testResult);567      // Fix from ansgarkonermann568      // invokedMethod is used in the finally, which can be invoked if569      // any of the test listeners throws an exception, therefore,570      // invokedMethod must have a value before we get here571      if (!m_suiteState.isFailed()) {572        runTestListeners(testResult);573      }574      log(3, "Invoking " + tm.getQualifiedName());575      runInvokedMethodListeners(BEFORE_INVOCATION, invokedMethod, testResult);576      m_notifier.addInvokedMethod(invokedMethod);577      Method thisMethod = tm.getConstructorOrMethod().getMethod();578      if (RuntimeBehavior.isDryRun()) {579        setTestStatus(testResult, ITestResult.SUCCESS);580        return testResult;581      }582      // If this method is a IHookable, invoke its run() method583      IHookable hookableInstance =584          IHookable.class.isAssignableFrom(tm.getRealClass())585              ? (IHookable) instance586              : m_configuration.getHookable();587      if (MethodHelper.calculateTimeOut(tm) <= 0) {588        if (hookableInstance != null) {589          MethodInvocationHelper.invokeHookable(590              instance, parameterValues, hookableInstance, thisMethod, testResult);591        } else {592          // Not a IHookable, invoke directly593          MethodInvocationHelper.invokeMethod(thisMethod, instance, parameterValues);594        }595        setTestStatus(testResult, ITestResult.SUCCESS);596      } else {597        // Method with a timeout598        MethodInvocationHelper.invokeWithTimeout(599            tm, instance, parameterValues, testResult, hookableInstance);600      }601    } catch (InvocationTargetException ite) {602      testResult.setThrowable(ite.getCause());603      setTestStatus(testResult, ITestResult.FAILURE);604    } catch (ThreadExecutionException tee) { // wrapper for TestNGRuntimeException605      Throwable cause = tee.getCause();606      if (TestNGRuntimeException.class.equals(cause.getClass())) {607        testResult.setThrowable(cause.getCause());608      } else {609        testResult.setThrowable(cause);610      }611      setTestStatus(testResult, ITestResult.FAILURE);612    } catch (Throwable thr) { // covers the non-wrapper exceptions613      testResult.setThrowable(thr);614      setTestStatus(testResult, ITestResult.FAILURE);615    } finally {616      // Set end time ASAP617      testResult.setEndMillis(System.currentTimeMillis());618      ExpectedExceptionsHolder expectedExceptionClasses =619          new ExpectedExceptionsHolder(620              m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));621      StatusHolder holder =622          considerExceptions(tm, testResult, expectedExceptionClasses, failureContext);623      int statusBeforeListenerInvocation = testResult.getStatus();624      runInvokedMethodListeners(AFTER_INVOCATION, invokedMethod, testResult);625      boolean wasResultUnaltered = statusBeforeListenerInvocation == testResult.getStatus();626      handleInvocationResults(tm, testResult, failureContext, holder, wasResultUnaltered);627      // If this method has a data provider and just failed, memorize the number628      // at which it failed.629      // Note: we're not exactly testing that this method has a data provider, just630      // that it has parameters, so might have to revisit this if bugs get reported631      // for the case where this method has parameters that don't come from a data632      // provider633      if (testResult.getThrowable() != null && parameterValues.length > 0) {634        tm.addFailedInvocationNumber(parametersIndex);635      }636      //637      // Increment the invocation count for this method638      //639      tm.incrementCurrentInvocationCount();640      runTestListeners(testResult);641      collectResults(tm, testResult);642      ITestNGMethod[] tearDownConfigMethods =643          TestNgMethodUtils.filterTeardownConfigurationMethods(tm, afterMethods);644      invokeConfigurations(645          testClass,646          tm,647          tearDownConfigMethods,648          suite,649          params,650          parameterValues,651          instance,652          testResult);653      //654      // Invoke afterGroups configurations655      //656      invokeAfterGroupsConfigurations(tm, groupMethods, suite, params, instance);657      // Reset the test result last. If we do this too early, Reporter.log()658      // invocations from listeners will be discarded659      Reporter.setCurrentTestResult(null);660    }661    return testResult;662  }663  private static void setTestStatus(ITestResult result, int status) {664    // set the test to success as long as the testResult hasn't been changed by the user via665    // Reporter.getCurrentTestResult666    if (result.getStatus() == ITestResult.STARTED) {667      result.setStatus(status);668    }669  }670  private void collectResults(ITestNGMethod testMethod, ITestResult result) {671    // Collect the results672    int status = result.getStatus();673    if (ITestResult.SUCCESS == status) {674      m_notifier.addPassedTest(testMethod, result);675    } else if (ITestResult.SKIP == status) {676      m_notifier.addSkippedTest(testMethod, result);677    } else if (ITestResult.FAILURE == status) {678      m_notifier.addFailedTest(testMethod, result);679    } else if (ITestResult.SUCCESS_PERCENTAGE_FAILURE == status) {680      m_notifier.addFailedButWithinSuccessPercentageTest(testMethod, result);681    } else {682      assert false : "UNKNOWN STATUS:" + status;683    }684  }685  /**686   * invokeTestMethods() eventually converge here to invoke a single @Test method.687   *688   * <p>This method is responsible for actually invoking the method. It decides if the invocation689   * must be done:690   *691   * <ul>692   *   <li>through an <code>IHookable</code>693   *   <li>directly (through reflection)694   *   <li>in a separate thread (in case it needs to timeout)695   * </ul>696   *697   * <p>This method is also responsible for698   * invoking @BeforeGroup, @BeforeMethod, @AfterMethod, @AfterGroup if it is the case for the699   * passed in @Test method.700   */701  ITestResult invokeTestMethod(702      Object instance,703      ITestNGMethod tm,704      Object[] parameterValues,705      int parametersIndex,706      XmlSuite suite,707      Map<String, String> params,708      ITestClass testClass,709      ITestNGMethod[] beforeMethods,710      ITestNGMethod[] afterMethods,711      ConfigurationGroupMethods groupMethods,712      FailureContext failureContext) {713    // Mark this method with the current thread id714    tm.setId(ThreadUtil.currentThreadInfo());715    return invokeMethod(716        instance,717        tm,718        parameterValues,719        parametersIndex,720        suite,721        params,722        testClass,723        beforeMethods,724        afterMethods,725        groupMethods,726        failureContext);727  }728  /**729   * Filter all the beforeGroups methods and invoke only those that apply to the current test method730   */731  private void invokeBeforeGroupsConfigurations(732      ITestNGMethod currentTestMethod,733      ConfigurationGroupMethods groupMethods,734      XmlSuite suite,735      Map<String, String> params,736      Object instance) {737    List<ITestNGMethod> filteredMethods = Lists.newArrayList();738    String[] groups = currentTestMethod.getGroups();739    for (String group : groups) {740      List<ITestNGMethod> methods = groupMethods.getBeforeGroupMethodsForGroup(group);741      if (methods != null) {742        filteredMethods.addAll(methods);743      }744    }745    ITestNGMethod[] beforeMethodsArray = filteredMethods.toArray(new ITestNGMethod[0]);746    //747    // Invoke the right groups methods748    //749    if (beforeMethodsArray.length > 0) {750      // don't pass the IClass or the instance as the method may be external751      // the invocation must be similar to @BeforeTest/@BeforeSuite752      invokeConfigurations(753          null, beforeMethodsArray, suite, params, /* no parameter values */ null, instance);754    }755    //756    // Remove them so they don't get run again757    //758    groupMethods.removeBeforeGroups(groups);759  }760  private void invokeAfterGroupsConfigurations(761      ITestNGMethod currentTestMethod,762      ConfigurationGroupMethods groupMethods,763      XmlSuite suite,764      Map<String, String> params,765      Object instance) {766    // Skip this if the current method doesn't belong to any group767    // (only a method that belongs to a group can trigger the invocation768    // of afterGroups methods)769    if (currentTestMethod.getGroups().length == 0) {770      return;771    }772    // See if the currentMethod is the last method in any of the groups773    // it belongs to774    Map<String, String> filteredGroups = Maps.newHashMap();775    String[] groups = currentTestMethod.getGroups();776    for (String group : groups) {777      if (groupMethods.isLastMethodForGroup(group, currentTestMethod)) {778        filteredGroups.put(group, group);779      }780    }781    if (filteredGroups.isEmpty()) {782      return;783    }784    // The list of afterMethods to run785    Map<ITestNGMethod, ITestNGMethod> afterMethods = Maps.newHashMap();786    // Now filteredGroups contains all the groups for which we need to run the afterGroups787    // method.  Find all the methods that correspond to these groups and invoke them.788    for (String g : filteredGroups.values()) {789      List<ITestNGMethod> methods = groupMethods.getAfterGroupMethodsForGroup(g);790      // Note:  should put them in a map if we want to make sure the same afterGroups791      // doesn't get run twice792      if (methods != null) {793        for (ITestNGMethod m : methods) {794          afterMethods.put(m, m);795        }796      }797    }798    // Got our afterMethods, invoke them799    ITestNGMethod[] afterMethodsArray = afterMethods.keySet().toArray(new ITestNGMethod[0]);800    // don't pass the IClass or the instance as the method may be external801    // the invocation must be similar to @BeforeTest/@BeforeSuite802    invokeConfigurations(803        null, afterMethodsArray, suite, params, /* no parameter values */ null, instance);804    // Remove the groups so they don't get run again805    groupMethods.removeAfterGroups(filteredGroups.keySet());806  }807  FailureContext retryFailed(808      Object instance,809      ITestNGMethod tm,810      Object[] paramValues,811      ITestClass testClass,812      ITestNGMethod[] beforeMethods,813      ITestNGMethod[] afterMethods,814      ConfigurationGroupMethods groupMethods,815      List<ITestResult> result,816      int failureCount,817      ITestContext testContext,818      Map<String, String> parameters,819      int parametersIndex) {820    FailureContext failure = new FailureContext();821    failure.count = failureCount;822    do {823      failure.instances = Lists.newArrayList();824      Map<String, String> allParameters = Maps.newHashMap();825      // TODO: This recreates all the parameters every time when we only need826      // one specific set. Should optimize it by only recreating the set needed.827      ParameterHandler handler = new ParameterHandler(m_annotationFinder, m_dataproviderListeners);828      ParameterBag bag = handler.createParameters(tm, parameters, allParameters, testContext);829      Object[] parameterValues =830          Parameters.getParametersFromIndex(bag.parameterHolder.parameters, parametersIndex);831      if (bag.parameterHolder.origin == ParameterHolder.ParameterOrigin.NATIVE) {832        parameterValues = paramValues;833      }834      result.add(835          invokeMethod(836              instance,837              tm,838              parameterValues,839              parametersIndex,840              testContext.getSuite().getXmlSuite(),841              allParameters,842              testClass,843              beforeMethods,844              afterMethods,845              groupMethods,846              failure));847    } while (!failure.instances.isEmpty());848    return failure;849  }850  /**851   * Invoke all the test methods. Note the plural: the method passed in parameter might be invoked852   * several times if the test class it belongs to has more than one instance (i.e., if an @Factory853   * method has been declared somewhere that returns several instances of this TestClass). If854   * no @Factory method was specified, testMethod will only be invoked once.855   *856   * <p>Note that this method also takes care of invoking the beforeTestMethod and afterTestMethod,857   * if any.858   *859   * <p>Note (alex): this method can be refactored to use a SingleTestMethodWorker that directly860   * invokes {@link #invokeTestMethod(Object, ITestNGMethod, Object[], int, XmlSuite, Map,861   * ITestClass, ITestNGMethod[], ITestNGMethod[], ConfigurationGroupMethods, FailureContext)} and862   * this would simplify the implementation (see how DataTestMethodWorker is used)863   */864  @Override865  public List<ITestResult> invokeTestMethods(866      ITestNGMethod testMethod,867      Map<String, String> testParameters,868      ConfigurationGroupMethods groupMethods,869      Object instance,870      ITestContext testContext) {871    // Potential bug here if the test method was declared on a parent class872    if (testMethod.getTestClass() == null) {873      throw new IllegalArgumentException(874          "COULDN'T FIND TESTCLASS FOR " + testMethod.getRealClass());875    }876    XmlSuite suite = testContext.getSuite().getXmlSuite();877    if (!MethodHelper.isEnabled(878        testMethod.getConstructorOrMethod().getMethod(), m_annotationFinder)) {879      // return if the method is not enabled. No need to do any more calculations880      return Collections.emptyList();881    }882    // By the time this testMethod to be invoked,883    // all dependencies should be already run or we need to skip this method,884    // so invocation count should not affect dependencies check885    String okToProceed = checkDependencies(testMethod, testContext.getAllTestMethods());886    if (okToProceed != null) {887      //888      // Not okToProceed. Test is being skipped889      //890      ITestResult result =891          registerSkippedTestResult(892              testMethod, System.currentTimeMillis(), new Throwable(okToProceed));893      m_notifier.addSkippedTest(testMethod, result);894      return Collections.singletonList(result);895    }896    Map<String, String> parameters =897        testMethod.findMethodParameters(testContext.getCurrentXmlTest());898    // For invocationCount > 1 and threadPoolSize > 1 run this method in its own pool thread.899    if (testMethod.getInvocationCount() > 1 && testMethod.getThreadPoolSize() > 1) {900      return invokePooledTestMethods(testMethod, suite, parameters, groupMethods, testContext);901    }902    long timeOutInvocationCount = testMethod.getInvocationTimeOut();903    // FIXME: Is this correct?904    boolean onlyOne = testMethod.getThreadPoolSize() > 1 || timeOutInvocationCount > 0;905    int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();906    ITestClass testClass = testMethod.getTestClass();907    List<ITestResult> result = Lists.newArrayList();908    FailureContext failure = new FailureContext();909    ITestNGMethod[] beforeMethods =910        TestNgMethodUtils.filterBeforeTestMethods(testClass, CAN_RUN_FROM_CLASS);911    ITestNGMethod[] afterMethods =912        TestNgMethodUtils.filterAfterTestMethods(testClass, CAN_RUN_FROM_CLASS);913    while (invocationCount-- > 0) {914      long start = System.currentTimeMillis();915      Map<String, String> allParameterNames = Maps.newHashMap();916      ParameterHandler handler = new ParameterHandler(m_annotationFinder, m_dataproviderListeners);917      ParameterBag bag =918          handler.createParameters(919              testMethod, parameters, allParameterNames, testContext, instance);920      if (bag.hasErrors()) {921        ITestResult tr = bag.errorResult;922        Throwable throwable = tr.getThrowable();923        if (throwable instanceof TestNGException) {924          tr.setStatus(ITestResult.FAILURE);925          m_notifier.addFailedTest(testMethod, tr);926        } else {927          tr.setStatus(ITestResult.SKIP);928          m_notifier.addSkippedTest(testMethod, tr);929        }930        runTestListeners(tr);931        result.add(tr);932        continue;933      }934      Iterator<Object[]> allParameterValues = bag.parameterHolder.parameters;935      int parametersIndex = 0;936      try {937        if (bag.runInParallel()) {938          List<TestMethodWithDataProviderMethodWorker> workers = Lists.newArrayList();939          while (allParameterValues.hasNext()) {940            Object[] next = allParameterValues.next();941            if (next == null) {942              // skipped value943              parametersIndex++;944              continue;945            }946            Object[] parameterValues =947                Parameters.injectParameters(948                    next, testMethod.getConstructorOrMethod().getMethod(), testContext);949            TestMethodWithDataProviderMethodWorker w =950                new TestMethodWithDataProviderMethodWorker(951                    this,952                    testMethod,953                    parametersIndex,954                    parameterValues,955                    instance,956                    parameters,957                    testClass,958                    beforeMethods,959                    afterMethods,960                    groupMethods,961                    testContext,962                    m_skipFailedInvocationCounts,963                    invocationCount,964                    failure.count,965                    m_notifier);966            workers.add(w);967            // testng387: increment the param index in the bag.968            parametersIndex++;969          }970          PoolService<List<ITestResult>> ps = new PoolService<>(suite.getDataProviderThreadCount());971          List<List<ITestResult>> r = ps.submitTasksAndWait(workers);972          for (List<ITestResult> l2 : r) {973            result.addAll(l2);974          }975        } else {976          while (allParameterValues.hasNext()) {977            Object[] next = allParameterValues.next();978            if (next == null) {979              // skipped value980              parametersIndex++;981              continue;982            }983            Object[] parameterValues =984                Parameters.injectParameters(985                    next, testMethod.getConstructorOrMethod().getMethod(), testContext);986            List<ITestResult> tmpResults = Lists.newArrayList();987            int tmpResultsIndex = -1;988            try {989              tmpResults.add(990                  invokeTestMethod(991                      instance,992                      testMethod,993                      parameterValues,994                      parametersIndex,995                      suite,996                      parameters,997                      testClass,998                      beforeMethods,999                      afterMethods,1000                      groupMethods,1001                      failure));1002              tmpResultsIndex++;1003            } finally {1004              boolean lastSucces = false;1005              if (tmpResultsIndex >= 0) {1006                lastSucces = (tmpResults.get(tmpResultsIndex).getStatus() == ITestResult.SUCCESS);1007              }1008              if (failure.instances.isEmpty() || lastSucces) {1009                result.addAll(tmpResults);1010              } else {1011                List<ITestResult> retryResults = Lists.newArrayList();1012                failure =1013                    retryFailed(1014                        instance,1015                        testMethod,1016                        parameterValues,1017                        testClass,1018                        beforeMethods,1019                        afterMethods,1020                        groupMethods,1021                        retryResults,1022                        failure.count,1023                        testContext,1024                        parameters,1025                        parametersIndex);1026                result.addAll(retryResults);1027              }1028              // If we have a failure, skip all the1029              // other invocationCounts1030              if (failure.count > 01031                  && (m_skipFailedInvocationCounts || testMethod.skipFailedInvocations())) {1032                while (invocationCount-- > 0) {1033                  result.add(1034                      registerSkippedTestResult(testMethod, System.currentTimeMillis(), null));1035                }1036              }1037            } // end finally1038            parametersIndex++;1039          }1040        }1041      } catch (Throwable cause) {1042        ITestResult r =1043            new TestResult(1044                testMethod.getTestClass(),1045                testMethod,1046                cause,1047                start,1048                System.currentTimeMillis(),1049                m_testContext);1050        r.setStatus(TestResult.FAILURE);1051        result.add(r);1052        runTestListeners(r);1053        m_notifier.addFailedTest(testMethod, r);1054      } // catch1055    }1056    return result;1057  }1058  private ITestResult registerSkippedTestResult(1059      ITestNGMethod testMethod, long start, Throwable throwable) {1060    ITestResult result =1061        new TestResult(1062            testMethod.getTestClass(),1063            testMethod,1064            throwable,1065            start,1066            System.currentTimeMillis(),1067            m_testContext);1068    if (RuntimeBehavior.invokeListenersForSkippedTests()) {1069      result.setStatus(ITestResult.STARTED);1070      runTestListeners(result);1071    }1072    result.setStatus(TestResult.SKIP);1073    Reporter.setCurrentTestResult(result);1074    runTestListeners(result);1075    return result;1076  }1077  /** Invokes a method that has a specified threadPoolSize. */1078  private List<ITestResult> invokePooledTestMethods(1079      ITestNGMethod testMethod,1080      XmlSuite suite,1081      Map<String, String> parameters,1082      ConfigurationGroupMethods groupMethods,1083      ITestContext testContext) {1084    //1085    // Create the workers1086    //1087    List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();1088    // Create one worker per invocationCount1089    for (int i = 0; i < testMethod.getInvocationCount(); i++) {1090      // we use clones for reporting purposes1091      ITestNGMethod clonedMethod = testMethod.clone();1092      clonedMethod.setInvocationCount(1);1093      clonedMethod.setThreadPoolSize(1);1094      MethodInstance mi = new MethodInstance(clonedMethod);1095      workers.add(new SingleTestMethodWorker(this, mi, parameters, testContext, m_classListeners));1096    }1097    return runWorkers(1098        testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite, parameters);1099  }1100  static class FailureContext {1101    int count = 0;1102    List<Object> instances = Lists.newArrayList();1103  }1104  private static class StatusHolder {1105    boolean handled = false;1106    int status;1107  }1108  private StatusHolder considerExceptions(1109      ITestNGMethod tm,1110      ITestResult testresult,1111      ExpectedExceptionsHolder exceptionsHolder,1112      FailureContext failure) {1113    StatusHolder holder = new StatusHolder();1114    holder.status = testresult.getStatus();1115    holder.handled = false;1116    Throwable ite = testresult.getThrowable();1117    if (holder.status == ITestResult.FAILURE && ite != null) {1118      //  Invocation caused an exception, see if the method was annotated with @ExpectedException1119      if (exceptionsHolder != null) {1120        if (exceptionsHolder.isExpectedException(ite)) {1121          testresult.setStatus(ITestResult.SUCCESS);1122          holder.status = ITestResult.SUCCESS;1123        } else {1124          if (isSkipExceptionAndSkip(ite)) {1125            holder.status = ITestResult.SKIP;1126          } else {1127            testresult.setThrowable(exceptionsHolder.wrongException(ite));1128            holder.status = ITestResult.FAILURE;1129          }1130        }1131      } else {1132        handleException(ite, tm, testresult, failure.count++);1133        holder.handled = true;1134        holder.status = testresult.getStatus();1135      }1136    } else if (holder.status != ITestResult.SKIP && exceptionsHolder != null) {1137      TestException exception = exceptionsHolder.noException(tm);1138      if (exception != null) {1139        testresult.setThrowable(exception);1140        holder.status = ITestResult.FAILURE;1141      }1142    }1143    return holder;1144  }1145  private void handleInvocationResults(1146      ITestNGMethod testMethod,1147      ITestResult testResult,1148      FailureContext failure,1149      StatusHolder holder,1150      boolean wasResultUnaltered) {1151    //1152    // Go through all the results and create a TestResult for each of them1153    //1154    List<ITestResult> resultsToRetry = Lists.newArrayList();1155    Throwable ite = testResult.getThrowable();1156    int status =1157        computeTestStatusComparingTestResultAndStatusHolder(testResult, holder, wasResultUnaltered);1158    boolean handled = holder.handled;1159    IRetryAnalyzer retryAnalyzer = testMethod.getRetryAnalyzer();1160    boolean willRetry =1161        retryAnalyzer != null1162            && status == ITestResult.FAILURE1163            && failure.instances != null1164            && retryAnalyzer.retry(testResult);1165    if (willRetry) {1166      resultsToRetry.add(testResult);1167      Object instance = testResult.getInstance();1168      if (!failure.instances.contains(instance)) {1169        failure.instances.add(instance);1170      }1171      testResult.setStatus(ITestResult.SKIP);1172      testResult.setWasRetried(true);1173    } else {1174      testResult.setStatus(status);1175      if (status == ITestResult.FAILURE && !handled) {1176        int count = failure.count++;1177        if (testMethod.isDataDriven()) {1178          count = 0;1179        }1180        handleException(ite, testMethod, testResult, count);1181      }1182    }1183  }1184  private static int computeTestStatusComparingTestResultAndStatusHolder(1185      ITestResult testResult, StatusHolder holder, boolean wasResultUnaltered) {1186    if (wasResultUnaltered) {1187      return holder.status;1188    }1189    return testResult.getStatus();1190  }1191  private boolean isSkipExceptionAndSkip(Throwable ite) {1192    return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip();1193  }1194  /**1195   * To reduce thread contention and also to correctly handle thread-confinement this method invokes1196   * the @BeforeGroups and @AfterGroups corresponding to the current @Test method.1197   */1198  private List<ITestResult> runWorkers(1199      ITestNGMethod testMethod,1200      List<IWorker<ITestNGMethod>> workers,1201      int threadPoolSize,1202      ConfigurationGroupMethods groupMethods,1203      XmlSuite suite,1204      Map<String, String> parameters) {1205    // Invoke @BeforeGroups on the original method (reduce thread contention,1206    // and also solve thread confinement)1207    ITestClass testClass = testMethod.getTestClass();1208    Object[] instances = testClass.getInstances(true);1209    for (Object instance : instances) {1210      invokeBeforeGroupsConfigurations(testMethod, groupMethods, suite, parameters, instance);1211    }1212    long maxTimeOut = -1; // 10 seconds1213    for (IWorker<ITestNGMethod> tmw : workers) {1214      long mt = tmw.getTimeOut();1215      if (mt > maxTimeOut) {1216        maxTimeOut = mt;1217      }1218    }1219    ThreadUtil.execute("methods", workers, threadPoolSize, maxTimeOut, true);1220    //1221    // Collect all the TestResults1222    //1223    List<ITestResult> result = Lists.newArrayList();1224    for (IWorker<ITestNGMethod> tmw : workers) {1225      if (tmw instanceof TestMethodWorker) {1226        result.addAll(((TestMethodWorker) tmw).getTestResults());1227      }1228    }1229    for (Object instance : instances) {1230      invokeAfterGroupsConfigurations(testMethod, groupMethods, suite, parameters, instance);1231    }1232    return result;1233  }1234  /**1235   * Checks to see of the test method has certain dependencies that prevents TestNG from executing1236   * it1237   *1238   * @param testMethod test method being checked for1239   * @return error message or null if dependencies have been run successfully1240   */1241  private String checkDependencies(ITestNGMethod testMethod, ITestNGMethod[] allTestMethods) {1242    // If this method is marked alwaysRun, no need to check for its dependencies1243    if (testMethod.isAlwaysRun()) {1244      return null;1245    }1246    // Any missing group?1247    if (testMethod.getMissingGroup() != null && !testMethod.ignoreMissingDependencies()) {1248      return "Method "1249          + testMethod1250          + " depends on nonexistent group \""1251          + testMethod.getMissingGroup()1252          + "\"";1253    }1254    // If this method depends on groups, collect all the methods that1255    // belong to these groups and make sure they have been run successfully1256    String[] groups = testMethod.getGroupsDependedUpon();1257    if (null != groups && groups.length > 0) {1258      // Get all the methods that belong to the group depended upon1259      for (String element : groups) {1260        ITestNGMethod[] methods =1261            MethodGroupsHelper.findMethodsThatBelongToGroup(1262                testMethod, m_testContext.getAllTestMethods(), element);1263        if (methods.length == 0 && !testMethod.ignoreMissingDependencies()) {1264          // Group is missing1265          return "Method " + testMethod + " depends on nonexistent group \"" + element + "\"";1266        }1267        if (!haveBeenRunSuccessfully(testMethod, methods)) {1268          return "Method "1269              + testMethod1270              + " depends on not successfully finished methods in group \""1271              + element1272              + "\"";1273        }1274      }1275    } // depends on groups1276    // If this method depends on other methods, make sure all these other1277    // methods have been run successfully1278    if (TestNgMethodUtils.cannotRunMethodIndependently(testMethod)) {1279      ITestNGMethod[] methods = MethodHelper.findDependedUponMethods(testMethod, allTestMethods);1280      if (!haveBeenRunSuccessfully(testMethod, methods)) {1281        return "Method " + testMethod + " depends on not successfully finished methods";1282      }1283    }1284    return null;1285  }1286  /** @return the test results that apply to one of the instances of the testMethod. */1287  private Set<ITestResult> keepSameInstances(ITestNGMethod method, Set<ITestResult> results) {1288    Set<ITestResult> result = Sets.newHashSet();1289    for (ITestResult r : results) {1290      Object o = method.getInstance();1291      // Keep this instance if 1) It's on a different class or 2) It's on the same class1292      // and on the same instance1293      Object instance = r.getInstance() != null ? r.getInstance() : r.getMethod().getInstance();1294      if (r.getTestClass() != method.getTestClass() || instance == o) result.add(r);1295    }1296    return result;1297  }1298  /** @return true if all the methods have been run successfully */1299  private boolean haveBeenRunSuccessfully(ITestNGMethod testMethod, ITestNGMethod[] methods) {1300    // Make sure the method has been run successfully1301    for (ITestNGMethod method : methods) {1302      Set<ITestResult> results = keepSameInstances(testMethod, m_notifier.getPassedTests(method));1303      Set<ITestResult> failedAndSkippedMethods = Sets.newHashSet();1304      Set<ITestResult> skippedAttempts = m_notifier.getSkippedTests(method);1305      failedAndSkippedMethods.addAll(m_notifier.getFailedTests(method));1306      failedAndSkippedMethods.addAll(skippedAttempts);1307      Set<ITestResult> failedresults = keepSameInstances(testMethod, failedAndSkippedMethods);1308      boolean wasMethodRetried = !results.isEmpty() && !skippedAttempts.isEmpty();1309      if (!wasMethodRetried && !failedresults.isEmpty()) {1310        // If failed results were returned on the same instance, then these tests didn't pass1311        return false;1312      }1313      for (ITestResult result : results) {1314        if (!result.isSuccess()) {1315          return false;1316        }1317      }1318    }1319    return true;1320  }1321  /**1322   * An exception was thrown by the test, determine if this method should be marked as a failure or1323   * as failure_but_within_successPercentage1324   */1325  private void handleException(1326      Throwable throwable, ITestNGMethod testMethod, ITestResult testResult, int failureCount) {1327    if (throwable != null && testResult.getThrowable() == null) {1328      testResult.setThrowable(throwable);1329    }1330    int successPercentage = testMethod.getSuccessPercentage();1331    int invocationCount = testMethod.getInvocationCount();1332    float numberOfTestsThatCanFail = ((100 - successPercentage) * invocationCount) / 100f;1333    if (failureCount < numberOfTestsThatCanFail) {1334      testResult.setStatus(ITestResult.SUCCESS_PERCENTAGE_FAILURE);1335    } else {1336      testResult.setStatus(ITestResult.FAILURE);1337    }1338  }1339  interface Predicate<K, T> {1340    boolean isTrue(K k, T v);1341  }1342  static class CanRunFromClassPredicate implements Predicate<ITestNGMethod, IClass> {...Source:ITestResult.java  
...42   * @return The throwable that was thrown while running the43   * method, or null if no exception was thrown.44   */45  public Throwable getThrowable();46  public void setThrowable(Throwable throwable);47  /**48   * @return the start date for this test, in milliseconds.49   */50  public long getStartMillis();51  /**52   * @return the end date for this test, in milliseconds.53   */54  public long getEndMillis();55  public void setEndMillis(long millis);56  /**57   * @return The name of this TestResult, typically identical to the name58   * of the method.59   */60  public String getName();...Source:ConnsTestListener.java  
...48					failureMsg.append(verificationFailures.get(i)).append("\n");49				}50				// set merged throwable51				Throwable merged = new Throwable(failureMsg.toString());52				testResult.setThrowable(merged);53			}54		}55	}56}
...setThrowable
Using AI Code Generation
1import org.testng.ITestResult;2public class TestResult {3    public static void setThrowable(ITestResult result, Throwable throwable) {4        result.setThrowable(throwable);5    }6}7import org.testng.ITestResult;8public class TestResult {9    public static Throwable getThrowable(ITestResult result) {10        return result.getThrowable();11    }12}13import org.testng.ITestResult;14public class TestResult {15    public static void setStatus(ITestResult result, int status) {16        result.setStatus(status);17    }18}19import org.testng.ITestResult;20public class TestResult {21    public static int getStatus(ITestResult result) {22        return result.getStatus();23    }24}25import org.testng.ITestResult;26public class TestResult {27    public static void setParameters(ITestResult result, Object[] parameters) {28        result.setParameters(parameters);29    }30}31import org.testng.ITestResult;32public class TestResult {33    public static Object[] getParameters(ITestResult result) {34        return result.getParameters();35    }36}37import org.testng.ITestResult;38public class TestResult {39    public static void setTestName(ITestResult result, String testname) {40        result.setTestName(testname);41    }42}43import org.testng.ITestResult;44public class TestResult {45    public static String getTestName(ITestResult result) {46        return result.getTestName();47    }48}49import org.testng.ITestResult;50public class TestResult {51    public static void setStartMillis(ITestResult result, long millis) {52        result.setStartMillis(millis);53    }54}55import org.testng.ITestResult;56public class TestResult {57    public static long getStartMillis(ITestResult result) {58        return result.getStartMillis();setThrowable
Using AI Code Generation
1public void onTestFailure(ITestResult tr) {2    tr.setThrowable(new Throwable("This is a custom failure message"));3}4public void onTestSkipped(ITestResult tr) {5    tr.setThrowable(new Throwable("This is a custom skip message"));6}7public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {8    tr.setThrowable(new Throwable("This is a custom failure within success percentage message"));9}10public void onTestSuccess(ITestResult tr) {11    tr.setThrowable(new Throwable("This is a custom success message"));12}13public void onTestStart(ITestResult tr) {14    tr.setThrowable(new Throwable("This is a custom start message"));15}16public void onTestFinish(ITestResult tr) {17    tr.setThrowable(new Throwable("This is a custom finish message"));18}19public void onStart(ITestContext tr) {20    tr.setThrowable(new Throwable("This is a custom start message"));21}22public void onFinish(ITestContext tr) {23    tr.setThrowable(new Throwable("This is a custom finish message"));24}25public void onExecutionStart() {26    ITestResult tr = Reporter.getCurrentTestResult();27    tr.setThrowable(new Throwable("This is a custom start message"));28}29public void onExecutionFinish() {30    ITestResult tr = Reporter.getCurrentTestResult();31    tr.setThrowable(new Throwable("This is a custom finish message"));32}33public void onConfigurationSuccess(ITestResult tr) {34    tr.setThrowable(new Throwable("This is a custom success message"));35}36public void onConfigurationFailure(ITestResult tr) {37    tr.setThrowable(new Throwable("setThrowable
Using AI Code Generation
1Class<?> iTestResultClass = Class.forName("org.testng.ITestResult");2Method method = iTestResultClass.getMethod("setThrowable", Throwable.class);3method.invoke(iTestResult, new Exception("Test Failed"));4ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));5ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));6ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));7ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));8ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));9ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));10ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));11ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));12ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));13ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));14ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test Failed"));15ITestResult.class.getMethod("setThrowable", Throwable.class).invoke(iTestResult, new Exception("Test FailedsetThrowable
Using AI Code Generation
1import org.testng.ITestResult;2public class TestResult implements ITestResult {3public static void setThrowable(Throwable throwable) {4}5}6import org.testng.TestNG;7public class TestNGTest {8public static void setThrowable(Throwable throwable) {9}10}11import org.testng.internal.TestResult;12public class TestResultTest {13public static void setThrowable(Throwable throwable) {14}15}16import org.testng.internal.ITestResult;17public class ITestResultTest {18public static void setThrowable(Throwable throwable) {19}20}21import org.testng.internal.IResultListener;22public class IResultListenerTest {23public static void setThrowable(Throwable throwable) {24}25}26import org.testng.internal.IResultMap;27public class IResultMapTest {28public static void setThrowable(Throwable throwable) {29}30}31import org.testng.internal.IResultListener2;32public class IResultListener2Test {33public static void setThrowable(Throwable throwable) {34}35}36import org.testng.internal.IResultListener3;37public class IResultListener3Test {38public static void setThrowable(Throwable throwable) {39}40}41import org.testng.internal.IResultListener4;42public class IResultListener4Test {43public static void setThrowable(Throwable throwable) {44}45}46import org.testng.internal.IResultListener5;47public class IResultListener5Test {48public static void setThrowable(Throwable throwable) {49}50}51import org.testng.internal.IResultListener6;52public class IResultListener6Test {53public static void setThrowable(Throwable throwable) {54}55}56import org.testng.internal.IResultListener7;57public class IResultListener7Test {58public static void setThrowable(Throwable throwable) {59}setThrowable
Using AI Code Generation
1public void test1() throws Exception {2    throw new Exception("Test Exception");3}4public void test2() {5    Assert.assertTrue(true);6}7public void test3() {8    Assert.assertTrue(false);9}10public void test4() {11    Assert.assertTrue(false);12}13public void test5() {14    Assert.assertTrue(false);15}16public void test6() throws Exception {17    throw new Exception("Test Exception");18}19public void onTestFailure(ITestResult tr) {20    tr.setThrowable(new Exception("Exception Occurred"));21}22public void onTestSuccess(ITestResult tr) {23    tr.setThrowable(new Exception("Exception Occurred"));24}25}26    at com.test.testng.ITestResultTest.onTestFailure(ITestResultTest.java:32)27    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1954)28    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1936)29    at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)30    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)31    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)32    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)33    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)34    at org.testng.TestRunner.privateRun(TestRunner.java:756)35    at org.testng.TestRunner.run(TestRunner.java:610)36    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)37    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)38    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)39    at org.testng.SuiteRunner.run(SuiteRunner.java:289)40    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)41    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)42    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1304)43    at org.testng.TestNG.runSuitesLocally(TestTestNG 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!!
