Java Code Examples for org.testng.ITestResult#setThrowable()

The following examples show how to use org.testng.ITestResult#setThrowable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QuickPerfTestNGListener.java    From quickperf with Apache License 2.0 7 votes vote down vote up
private void executeTestMethodInNewJvmAndRecordPerformance(ITestResult testResult, TestExecutionContext testExecutionContext) {

        Object[] args = new Object[0];
        Method method = extractTestMethod(testResult);

        performanceRecording.start(testExecutionContext);

        try {
            Object target = testResult.getInstance();
            //directly invoke the method to lower the interaction between JUnit, other extensions and QuickPerf.
            method.invoke(target, args);
        } catch (Throwable throwable) {
            testResult.setThrowable(throwable);
            testResult.setStatus(ITestResult.FAILURE);
        } finally {
            performanceRecording.stop(testExecutionContext);
        }

    }
 
Example 2
Source File: ReportManager.java    From difido-reports with Apache License 2.0 6 votes vote down vote up
void onTestSuccess(ITestResult result) {
	if (!failureMessages.isEmpty()) {
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (String message : failureMessages) {
			if (first) {
				first = false;
			} else {
				sb.append(",\n");
			}
			sb.append(message);
		}
		result.setStatus(2);
		result.setThrowable(new AssertionError(sb.toString()));
		onTestFailure(result);
		return;
	}

	for (Reporter reporter : reporters) {
		reporter.onTestSuccess(result);
	}

}
 
Example 3
Source File: SingleConsumerQueueTest.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
  try {
    if (testResult.isSuccess()) {
      for (Object param : testResult.getParameters()) {
        if (param instanceof SingleConsumerQueue<?>) {
          assertThat((SingleConsumerQueue<?>) param, is(validate()));
        }
      }
    }
  } catch (AssertionError caught) {
    testResult.setStatus(ITestResult.FAILURE);
    testResult.setThrowable(caught);
  } finally {
    cleanUp(testResult);
  }
}
 
Example 4
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 6 votes vote down vote up
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
  try {
    if (testResult.isSuccess()) {
      validate(testResult);
    } else {
      if (!detailedParams.get()) {
        detailedParams.set(true);
      }
      testResult.setThrowable(new AssertionError(getTestName(method), testResult.getThrowable()));
    }
  } catch (Throwable caught) {
    testResult.setStatus(ITestResult.FAILURE);
    testResult.setThrowable(new AssertionError(getTestName(method), caught));
  } finally {
    cleanUp(testResult);
  }
}
 
Example 5
Source File: QuickPerfTestNGListener.java    From quickperf with Apache License 2.0 5 votes vote down vote up
@Override
public void run(IHookCallBack hookCallBack, ITestResult testResult) {

    TestExecutionContext testExecutionContext = buildTestExecutionContext(testResult);

    if(testExecutionContext.isQuickPerfDisabled()) {
        hookCallBack.runTestMethod(testResult);
        return;
    }

    if(SystemProperties.TEST_CODE_EXECUTING_IN_NEW_JVM.evaluate()) {
        executeTestMethodInNewJvmAndRecordPerformance(testResult, testExecutionContext);
        return;
    }

    JvmOrTestIssue jvmOrTestIssue =
            executeTestMethodAndRecordPerformance(hookCallBack, testResult, testExecutionContext);

    SetOfAnnotationConfigs testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();
    Collection<PerfIssuesToFormat> groupOfPerfIssuesToFormat = perfIssuesEvaluator.evaluatePerfIssuesIfNoJvmIssue(testAnnotationConfigs
                                                                                                                , testExecutionContext
                                                                                                                , jvmOrTestIssue);

    testExecutionContext.cleanResources();

    try {
        quickPerfReporter.report(jvmOrTestIssue, groupOfPerfIssuesToFormat, testExecutionContext);
    } catch (Throwable throwable) {
        testResult.setThrowable(throwable);
        testResult.setStatus(ITestResult.FAILURE);
    }

}
 
Example 6
Source File: TestNGTestResultProcessorAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String calculateTestCaseName(ITestResult iTestResult) {
    Object[] parameters = iTestResult.getParameters();
    String name = iTestResult.getName();
    if (parameters != null && parameters.length > 0) {
        StringBuilder builder = new StringBuilder(name).
                append("[").
                append(iTestResult.getMethod().getCurrentInvocationCount()).
                append("]");

        StringBuilder paramsListBuilder = new StringBuilder("(");
        int i = 0;
        for (Object parameter : parameters) {
            if (parameter == null) {
                paramsListBuilder.append("null");
            } else {
                try {
                    paramsListBuilder.append(parameter.toString());
                } catch (Exception e) {
                    // This may be thrown by the caller of this method at a later time
                    iTestResult.setThrowable(new UnrepresentableParameterException(iTestResult, i, e));
                    return builder.toString();
                }
            }
            if (++i < parameters.length) {
                paramsListBuilder.append(", ");
            }
        }
        paramsListBuilder.append(")");
        return builder.append(paramsListBuilder.toString()).toString();
    } else {
        return name;
    }
}
 
Example 7
Source File: TestNGTestResultProcessorAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String calculateTestCaseName(ITestResult iTestResult) {
    Object[] parameters = iTestResult.getParameters();
    String name = iTestResult.getName();
    if (parameters != null && parameters.length > 0) {
        StringBuilder builder = new StringBuilder(name).
                append("[").
                append(iTestResult.getMethod().getCurrentInvocationCount()).
                append("]");

        StringBuilder paramsListBuilder = new StringBuilder("(");
        int i = 0;
        for (Object parameter : parameters) {
            if (parameter == null) {
                paramsListBuilder.append("null");
            } else {
                try {
                    paramsListBuilder.append(parameter.toString());
                } catch (Exception e) {
                    // This may be thrown by the caller of this method at a later time
                    iTestResult.setThrowable(new UnrepresentableParameterException(iTestResult, i, e));
                    return builder.toString();
                }
            }
            if (++i < parameters.length) {
                paramsListBuilder.append(", ");
            }
        }
        paramsListBuilder.append(")");
        return builder.append(paramsListBuilder.toString()).toString();
    } else {
        return name;
    }
}
 
Example 8
Source File: TestNGTestResultProcessorAdapter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String calculateTestCaseName(ITestResult iTestResult) {
    Object[] parameters = iTestResult.getParameters();
    String name = iTestResult.getName();
    if (parameters != null && parameters.length > 0) {
        StringBuilder builder = new StringBuilder(name).
                append("[").
                append(iTestResult.getMethod().getCurrentInvocationCount()).
                append("]");

        StringBuilder paramsListBuilder = new StringBuilder("(");
        int i = 0;
        for (Object parameter : parameters) {
            if (parameter == null) {
                paramsListBuilder.append("null");
            } else {
                try {
                    paramsListBuilder.append(parameter.toString());
                } catch (Exception e) {
                    // This may be thrown by the caller of this method at a later time
                    iTestResult.setThrowable(new UnrepresentableParameterException(iTestResult, i, e));
                    return builder.toString();
                }
            }
            if (++i < parameters.length) {
                paramsListBuilder.append(", ");
            }
        }
        paramsListBuilder.append(")");
        return builder.append(paramsListBuilder.toString()).toString();
    } else {
        return name;
    }
}
 
Example 9
Source File: TestNGTestResultProcessorAdapter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private String calculateTestCaseName(ITestResult iTestResult) {
    Object[] parameters = iTestResult.getParameters();
    String name = iTestResult.getName();
    if (parameters != null && parameters.length > 0) {
        StringBuilder builder = new StringBuilder(name).
                append("[").
                append(iTestResult.getMethod().getCurrentInvocationCount()).
                append("]");

        StringBuilder paramsListBuilder = new StringBuilder("(");
        int i = 0;
        for (Object parameter : parameters) {
            if (parameter == null) {
                paramsListBuilder.append("null");
            } else {
                try {
                    paramsListBuilder.append(parameter.toString());
                } catch (Exception e) {
                    // This may be thrown by the caller of this method at a later time
                    iTestResult.setThrowable(new UnrepresentableParameterException(iTestResult, i, e));
                    return builder.toString();
                }
            }
            if (++i < parameters.length) {
                paramsListBuilder.append(", ");
            }
        }
        paramsListBuilder.append(")");
        return builder.append(paramsListBuilder.toString()).toString();
    } else {
        return name;
    }
}
 
Example 10
Source File: AbstractTest.java    From concurrentlinkedhashmap with Apache License 2.0 5 votes vote down vote up
@AfterMethod(alwaysRun = true)
public void validateIfSuccessful(ITestResult result) {
  try {
    if (result.isSuccess()) {
      for (Object param : result.getParameters()) {
        validate(param);
      }
    }
  } catch (AssertionError caught) {
    result.setStatus(ITestResult.FAILURE);
    result.setThrowable(caught);
  }
  initMocks(this);
}
 
Example 11
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Free memory by clearing unused resources after test execution. */
private void cleanUp(ITestResult testResult) {
  boolean briefParams = !detailedParams.get();

  if (testResult.isSuccess() && briefParams) {
    testResult.setParameters(EMPTY_PARAMS);
    testResult.setThrowable(null);
  }

  Object[] params = testResult.getParameters();
  for (int i = 0; i < params.length; i++) {
    Object param = params[i];
    if ((param instanceof AsyncCache<?, ?>) || (param instanceof Cache<?, ?>)
        || (param instanceof Map<?, ?>) || (param instanceof Eviction<?, ?>)
        || (param instanceof Expiration<?, ?>) || (param instanceof VarExpiration<?, ?>)
        || ((param instanceof CacheContext) && briefParams)) {
      params[i] = simpleNames.get(param.getClass(), key -> ((Class<?>) key).getSimpleName());
    } else if (param instanceof CacheContext) {
      params[i] = simpleNames.get(param.toString(), Object::toString);
    } else {
      params[i] = Objects.toString(param);
    }
  }

  /*
  // Enable in TestNG 7.0
  if ((testResult.getName() != null) && briefParams) {
    testResult.setTestName(simpleNames.get(testResult.getName(), Object::toString));
  }
  */

  CacheSpec.interner.get().clear();
}
 
Example 12
Source File: SeleniumTestHandler.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
/** Is invoked when test or configuration is finished. */
private void onTestFinish(ITestResult result) {
  // do not treat SeleniumTestHandler error as test failure
  if (testsWithFailure.containsKey(result.getTestClass().getRealClass().getName())
      && testsWithFailure
          .get(result.getTestClass().getRealClass().getName())
          .getMethod()
          .equals(result.getMethod())
      && result.getMethod().getCurrentInvocationCount() == 1) {
    // restore initial test exception
    result.setThrowable(
        testsWithFailure.get(result.getTestClass().getRealClass().getName()).getThrowable());
    return;
  }

  if (result.getStatus() == ITestResult.FAILURE || result.getStatus() == ITestResult.SKIP) {
    switch (result.getStatus()) {
      case ITestResult.FAILURE:
        if (result.getMethod().isTest()) {
          String errorDetails =
              result.getThrowable() != null
                  ? " Error: " + result.getThrowable().getLocalizedMessage()
                  : "";

          LOG.error("Test {} failed.{}", getCompletedTestLabel(result.getMethod()), errorDetails);
          LOG.debug(result.getThrowable().getLocalizedMessage(), result.getThrowable());

          testsWithFailure.put(result.getTestClass().getRealClass().getName(), result);
        }

        captureWebDriver(result);
        captureTestWorkspaceLogs(result);

        break;

      case ITestResult.SKIP:
        String skipReasonDetails =
            result.getThrowable() != null
                ? " The reason: " + result.getThrowable().getLocalizedMessage()
                : "";
        if (result.getMethod().isTest()) {
          LOG.warn(
              "Test {} skipped.{}", getCompletedTestLabel(result.getMethod()), skipReasonDetails);
        }

        // don't capture test data if test is skipped because of previous test with higher
        // priority failed
        if (testsWithFailure.containsKey(result.getMethod().getInstance().getClass().getName())) {
          return;
        }

        break;

      default:
    }
  }
}