org.junit.runner.notification.Failure Java Examples

The following examples show how to use org.junit.runner.notification.Failure. 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: CdiTestSuiteRunner.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public void testFailure(Failure failure) throws Exception
{
    Level level = this.logger.getLevel();

    this.logger.setLevel(Level.INFO);

    if (TRUE.equals(IS_CDI_TEST_RUNNER_EXECUTION.get()))
    {
        Description description = failure.getDescription();
        this.logger.info("[failed] " + description.getClassName() + "#" + description.getMethodName() +
            " message: " + failure.getMessage());
    }

    try
    {
        super.testFailure(failure);
    }
    finally
    {
        this.logger.setLevel(level);
    }
}
 
Example #2
Source File: TestCasesListener.java    From nopol with GNU General Public License v2.0 6 votes vote down vote up
private void logTestRunFinished(Result result) {
    Collection<String> lines = MetaList.newArrayList();
    lines.add(format("Tests run finished (%d ms)", result.getRunTime()));
    lines.add("<> Total tests run: " + result.getRunCount());
    lines.add("<> Ignored tests: " + result.getIgnoreCount());
    lines.add("<> Failed tests: " + result.getFailureCount());
    for (Failure failure : result.getFailures()) {
        lines.add("~ " + failure.getTestHeader());
        lines.add("[" + failure.getMessage() + "]");
        Throwable exception = failure.getException();
        lines.add(exception.toString());
        for (int i = 0; i <= 5; i += 1) {
            StackTraceElement element = exception.getStackTrace()[i];
            lines.add("    at " + element.toString());
        }
    }
    //logDebug(logger(), lines);
}
 
Example #3
Source File: TestTimedOutTestsListener.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 500)
public void testThreadDumpAndDeadlocks() throws Exception {
    new Deadlock();
    String s = null;
    while (true) {
        s = TimedOutTestsListener.buildDeadlockInfo();
        if (s != null) {
            break;
        }
        Thread.sleep(100);
    }

    Assert.assertEquals(3, countStringOccurrences(s, "BLOCKED"));

    Failure failure = new Failure(null, new Exception(TimedOutTestsListener.TEST_TIMED_OUT_PREFIX));
    StringWriter writer = new StringWriter();
    new TimedOutTestsListener(new PrintWriter(writer)).testFailure(failure);
    String out = writer.toString();

    Assert.assertTrue(out.contains("THREAD DUMP"));
    Assert.assertTrue(out.contains("DEADLOCKS DETECTED"));

    System.out.println(out);
}
 
Example #4
Source File: WebTauRunner.java    From webtau with Apache License 2.0 6 votes vote down vote up
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
    JavaBasedTest javaBasedTest = createJavaBasedTest(method);
    WebTauTest webTauTest = javaBasedTest.getTest();

    notifier.addListener(new RunListener() {
        @Override
        public void testFailure(Failure failure) {
            webTauTest.setExceptionIfNotSet(failure.getException());
        }
    });

    beforeTestRun(javaBasedTest);
    try {
        super.runChild(method, notifier);
    } catch (Throwable e) {
        webTauTest.setExceptionIfNotSet(e);
        throw e;
    } finally {
        afterTestRun(javaBasedTest);
    }
}
 
Example #5
Source File: JUnitRunner.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  try {
    H2O.main(args);
    TestUtil.stall_till_cloudsize(3);
    List<Class> tests = JUnitRunner.all();
    Result r = org.junit.runner.JUnitCore.runClasses(tests.toArray(new Class[0]));
    if( r.getFailureCount() == 0 ) {
      System.out.println("Successfully ran the following tests in " + (r.getRunTime() / 1000) + "s");
      for( Class c : tests )
        System.out.println(c.getName());
    } else {
      for( Failure f : r.getFailures() ) {
        System.err.println(f.getDescription());
        if( f.getException() != null )
          f.getException().printStackTrace();
      }
    }
    System.exit(r.getFailureCount());
  } catch( Throwable t ) {
    t.printStackTrace();
    System.exit(1);
  }
}
 
Example #6
Source File: JUnitRunListener.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static Throwable getTimeoutException(Failure failure) {
  if (failure == null) {
    return null;
  }
  final Throwable throwable = failure.getException();
  if (throwable.getClass() != TIMEOUT_EXCEPTION.getClass()) {
    return null;
  }
  final String message = throwable.getMessage();
  if (message == null || !message.startsWith(TIMEOUT_EXCEPTION_PREFIX)) {
    return null;
  }
  return throwable;
}
 
Example #7
Source File: TeamCityListener.java    From dekaf with Apache License 2.0 5 votes vote down vote up
@Override
public void testFailure(Failure f) throws Exception {
  Description d = f.getDescription();
  say("##teamcity[testFailed name='%s' message='%s' details='%s']",
      getTestName(d),
      f.getMessage(),
      f.getTrace());
}
 
Example #8
Source File: FeatureInfo.java    From karate with MIT License 5 votes vote down vote up
@Override
public void afterScenario(ScenarioResult result, ScenarioContext context) {
    // if dynamic scenario outline background or a call
    if (notifier == null || context.callDepth > 0) {
        return;
    }
    Description scenarioDescription = getScenarioDescription(result.getScenario());
    if (result.isFailed()) {
        notifier.fireTestFailure(new Failure(scenarioDescription, result.getError()));
    }
    // apparently this method should be always called
    // even if fireTestFailure was called
    notifier.fireTestFinished(scenarioDescription);
}
 
Example #9
Source File: ManualSettingsXmlTestCase.java    From arquillian-container-chameleon with Apache License 2.0 5 votes vote down vote up
@Test
public void runTestWithoutSettingsXmlFileSet() throws InitializationError, IOException {
    // set system properties
    System.setProperty("arquillian.xml", arqXmlWithEap);
    System.setProperty("arquillian.launch", "chameleon-eap");

    // run test
    List<Failure> failures = JUnitCore.runClasses(TestClass.class).getFailures();
    // it should have failed
    assertThat(failures).as("The test should have failed but it didn't").isNotEmpty();
    assertThat(localRepo).doesNotExist();
}
 
Example #10
Source File: ZeroCodeLoadRunner.java    From zerocode with Apache License 2.0 5 votes vote down vote up
@Override
protected void runChild(TestMapping child, RunNotifier notifier) {
    notifier.fireTestStarted(testDescription);

    boolean hasFailed = loadProcessor
            .addTest(child.testClass(), child.testMethod())
            .process();

    if(hasFailed){
        String failureMessage = testClass.getName() + "." + child.testMethod() + " Failed";
        LOGGER.error(failureMessage + ". See target/logs -or- junit granular failure report(csv) -or- fuzzy search and filter report(html) for details");
        notifier.fireTestFailure(new Failure(testDescription, new RuntimeException(failureMessage)));
    }
    notifier.fireTestFinished(testDescription);
}
 
Example #11
Source File: TestRunner.java    From spoon-examples with GNU General Public License v2.0 5 votes vote down vote up
public static List<Failure> runTest(String fullQualifiedName, String testCaseName, String[] classpath) throws MalformedURLException, ClassNotFoundException {
    ClassLoader classLoader = new URLClassLoader(
            arrayStringToArrayUrl.apply(classpath),
            ClassLoader.getSystemClassLoader()
    );
    Request request = Request.method(classLoader.loadClass(fullQualifiedName), testCaseName);
    Runner runner = request.getRunner();
    RunNotifier fNotifier = new RunNotifier();
    final TestListener listener = new TestListener();
    fNotifier.addFirstListener(listener);
    fNotifier.fireTestRunStarted(runner.getDescription());
    runner.run(fNotifier);
    return listener.getTestFails();
}
 
Example #12
Source File: IltConsumerJUnitListener.java    From joynr with Apache License 2.0 5 votes vote down vote up
public void testFailure(Failure failure) {
    String fullTestClassName;
    String baseTestClassName;

    logger.info(">>> testFailure called");

    Description description = failure.getDescription();
    printDescription(description, 1);
    logger.info("- failure.getException() = " + failure.getException());
    logger.info("- failure.getMessage() = " + failure.getMessage());
    logger.info("- failure.getTestHeader() = " + failure.getTestHeader());
    logger.info("- failure.getTrace() = " + failure.getTrace());

    if (description == null || description.getDisplayName() == null) {
        logger.info("<<< testFinished called");
        return;
    }

    // should have been created already in previous call to testStarted
    TestSuiteResultsStore store = getStore(description);

    TestCaseFailure testCaseFailure = new TestCaseFailure(failure.getMessage(), // message
                                                          failure.getException().toString(), // type
                                                          failure.getTrace() // text
    );
    TestCaseResult testCaseResult = new TestCaseResult(getTestCaseName(description),
                                                       getTestSuiteClassName(description),
                                                       null, // test not finished yet, will be updated later
                                                       "failed", // status
                                                       testCaseFailure, // failure
                                                       null // no systemOut
    );
    store.testCaseResults.add(testCaseResult);
    store.failures++;
    // everything else will be done in testFinished, which is also
    // called for failed tests as well.

    logger.info("<<< testFailure called");
}
 
Example #13
Source File: ArchUnitRunnerRunsRuleFieldsTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
static void verifyTestFinishedSuccessfully(RunNotifier runNotifier, ArgumentCaptor<Description> descriptionCaptor,
        String expectedDescriptionMethodName) {
    verify(runNotifier, never()).fireTestFailure(any(Failure.class));
    verify(runNotifier).fireTestFinished(descriptionCaptor.capture());
    Description description = descriptionCaptor.getValue();
    assertThat(description.getMethodName()).isEqualTo(expectedDescriptionMethodName);
}
 
Example #14
Source File: EnclosedNonStaticTest.java    From havarunner with MIT License 5 votes vote down vote up
@Test
public void HavaRunner_gives_a_helpful_error_if_a_suite_member_contains_nonstatic_inner_classes() {
    Failure failure = runAndRecordFailure(new HavaRunner(Suite.class));
    assertEquals(NonStaticInnerClassException.class, failure.getException().getClass());
    assertEquals(
        "The class com.github.havarunner.enclosed.EnclosedNonStaticTest$SuiteMember$InnerSuiteClass must be static (HavaRunner does not support non-static inner classes)",
        failure.getMessage()
    );
}
 
Example #15
Source File: ArchUnitRunnerRunsMethodsTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
public void executes_private_test_methods() {
    runner.runChild(getRule(privateTest, runner), runNotifier);

    verify(runNotifier, never()).fireTestFailure(any(Failure.class));
    verify(runNotifier).fireTestFinished(descriptionCaptor.capture());

    assertThat(descriptionCaptor.getAllValues()).extractingResultOf("getMethodName")
            .contains(privateTest);
}
 
Example #16
Source File: JpaUnitRunnerTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassWithoutPersistenceContextField() throws Exception {
    // GIVEN
    final JCodeModel jCodeModel = new JCodeModel();
    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
    final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
    jAnnotationUse.param("value", JpaUnitRunner.class);
    final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
    jMethod.annotate(Test.class);

    buildModel(testFolder.getRoot(), jCodeModel);
    compileModel(testFolder.getRoot());

    final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
    final RunListener listener = mock(RunListener.class);
    final RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);

    final JpaUnitRunner runner = new JpaUnitRunner(cut);

    // WHEN
    runner.run(notifier);

    // THEN
    final ArgumentCaptor<Failure> failureCaptor = ArgumentCaptor.forClass(Failure.class);
    verify(listener).testFailure(failureCaptor.capture());

    final Failure failure = failureCaptor.getValue();
    assertThat(failure.getException().getClass(), equalTo(IllegalArgumentException.class));
    assertThat(failure.getException().getMessage(), containsString("EntityManagerFactory or EntityManager field annotated"));
}
 
Example #17
Source File: DisablablePaxExam.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public void run(final RunNotifier notifier) {
    if (!SystemUtils.JAVA_VERSION.startsWith("1.8.")) {
        notifier.fireTestAssumptionFailed(new Failure(
                Description.createSuiteDescription(clazz),
                new IllegalStateException("Java " + SystemUtils.JAVA_VERSION + " not yet supported")));
    } else {
        super.run(notifier);
    }
}
 
Example #18
Source File: ForkedPinpointPluginTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private String failureToString(Failure failure) {
    StringBuilder builder = new StringBuilder();
    
    builder.append(failure.getTestHeader());
    builder.append(JUNIT_OUTPUT_DELIMITER);
    
    Throwable t = failure.getException();
    
    while (true) {
        builder.append(t.getClass().getName());
        builder.append(JUNIT_OUTPUT_DELIMITER);
        builder.append(t.getMessage());
        builder.append(JUNIT_OUTPUT_DELIMITER);

        for (StackTraceElement e : failure.getException().getStackTrace()) {
            builder.append(e.getClassName());
            builder.append(',');
            builder.append(e.getMethodName());
            builder.append(',');
            builder.append(e.getFileName());
            builder.append(',');
            builder.append(e.getLineNumber());
            
            builder.append(JUNIT_OUTPUT_DELIMITER);
        }
    
        Throwable cause = t.getCause();
        
        if (cause == null || t == cause) {
            break;
        }
        
        t = cause;
        builder.append("$CAUSE$");
        builder.append(JUNIT_OUTPUT_DELIMITER);
    }
    
    return builder.toString();
}
 
Example #19
Source File: MessageUtilTestJUnitRunner.java    From javase with MIT License 5 votes vote down vote up
public static void main(String[] args) {
   Result result = JUnitCore.runClasses(MessageUtilTestJUnit.class);
		
   for (Failure failure : result.getFailures()) {
      System.out.println(failure.toString());
   }
		
   System.out.println(result.wasSuccessful());
}
 
Example #20
Source File: PtlBlockJUnit4ClassRunnerWithParametersTest.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
/**
 * {@link ParameterizedBeforeClass}を設定したメソッドのバリデーションテスト
 */
@Test
public void testValidateParameterizedBeforeClass() throws Exception {
	Result result = JUnitCore.runClasses(ValidateParameterizedBeforeClass.class);
	assertThat(result.getFailureCount(), is(3));

	for (Failure failure : result.getFailures()) {
		assertThat(
				failure.getMessage(),
				is(anyOf(equalTo("Method beforeClass() should be static"),
						equalTo("Method beforeClass() should be public"),
						//							equalTo("Method beforeClass() should have 1 parameters"),
						equalTo("Method beforeClass() should be void"))));
	}
}
 
Example #21
Source File: JUnitFailureHackerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotAppendWhenNullWarnings() throws Exception {
    RuntimeException ex = new RuntimeException("foo");
    Failure failure = new Failure(Description.EMPTY, ex);
    
    //when
    hacker.appendWarnings(failure, null);
    
    //then
    assertEquals(ex, failure.getException());        
}
 
Example #22
Source File: TestSetupTeardownChaining.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Verify super method calls on {@link LuceneTestCase#tearDown()}.
 */
@Test
public void testTeardownChaining() {
  Result result = JUnitCore.runClasses(NestedTeardownChain.class);
  Assert.assertEquals(1, result.getFailureCount());
  Failure failure = result.getFailures().get(0);
  Assert.assertTrue(failure.getMessage()
      .contains("One of the overrides of tearDown does not propagate the call."));
}
 
Example #23
Source File: KurentoRunNotifier.java    From kurento-java with Apache License 2.0 5 votes vote down vote up
private void fireTestFailures(List<RunListener> listeners, final List<Failure> failures) {
  if (!failures.isEmpty()) {
    new SafeNotifier(listeners) {
      @Override
      protected void notifyListener(RunListener listener) throws Exception {
        for (Failure each : failures) {
          listener.testFailure(each);
        }
      }
    }.run();
  }
}
 
Example #24
Source File: AllureJunit4.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public void testFailure(final Failure failure) {
    final String uuid = testCases.get();
    getLifecycle().updateTestCase(uuid, testResult -> testResult
            .setStatus(getStatus(failure.getException()).orElse(null))
            .setStatusDetails(getStatusDetails(failure.getException()).orElse(null))
    );
}
 
Example #25
Source File: IltConsumerJUnitListener.java    From joynr with Apache License 2.0 5 votes vote down vote up
public void testAssumptionFailure(Failure failure) {
    logger.info(">>> testAssumptionFailure called");
    Description description = failure.getDescription();
    printDescription(description, 1);
    // should have been created already in previous call to testStarted
    TestSuiteResultsStore store = getStore(description);
    store.errors++;
    logger.info("<<< testAssumptionFailure called");
}
 
Example #26
Source File: AllureMarathonRunListener.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public void testFailure(Failure failure) {
    if (failure.getDescription().isTest()) {
        fireTestCaseFailure(failure.getException());
    } else {
        startFakeTestCase(failure.getDescription());
        fireTestCaseFailure(failure.getException());
        finishFakeTestCase();
    }
}
 
Example #27
Source File: OpenEJBJUnitDebugListener.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void testAssumptionFailure(final Failure failure) {
    try {
        doStop();
    } catch (final InterruptedException e) {
        Thread.interrupted();
    }
}
 
Example #28
Source File: JUnitListener.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Called when an atomic test fails
 */
@Override
public void testFailure(Failure failure) {
  // --- org.apache.commons.lang.EntitiesPerformanceTest::testEscapePrimitive
  System.out.println("--- " + failure.getDescription().getClassName() + "::" + failure.getDescription().getMethodName());
  System.out.println(failure.getTrace());
}
 
Example #29
Source File: ScottRunListener.java    From scott with MIT License 5 votes vote down vote up
@Override
public void testFailure(Failure failure) throws Exception {
	String testClassName = description.getTestClass() != null ? description.getTestClass().getTypeName() : null;
	String testMethodName = description.getMethodName();
	String scottReport = FailureRenderer.render(testClassName, testMethodName, failure.getException());
	setExceptionMessage(failure.getException(), scottReport);
	super.testFailure(failure);
}
 
Example #30
Source File: DisablablePaxExam.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public void run(final RunNotifier notifier) {
    if (!SystemUtils.JAVA_VERSION.startsWith("1.8.")) {
        notifier.fireTestAssumptionFailed(new Failure(
                Description.createSuiteDescription(clazz),
                new IllegalStateException("Java " + SystemUtils.JAVA_VERSION + " not yet supported")));
    } else {
        super.run(notifier);
    }
}