Anton
Anton

Reputation: 441

Is it possible to get test status in JUnit from RunListener.testFinished?

I need to process failed and passed tests differently in JUnit. But JUnit RunListener has only testFinished() that called whenever test failed or not, and testFailed() that is called only on failed tests.

Is there a way to find out test result (fail\pass) from RunListener.testFinished()?

Also, I was looking into another JUnit class -- TestWatcher. It has succeeded() method. But I don't want to use it because I also need to perform some actions when all tests are done ( testRunFinished() in RunListener)

Upvotes: 1

Views: 1663

Answers (1)

Kevin Klassen
Kevin Klassen

Reputation: 56

A way to do this with a RunListener class is to mark the failed tests with a child description in the testFailure() method and then check for that child description in testFinished() method:

class MyRunListener extends RunListener {
  private static final Description FAILED = Description.createTestDescription('failed', 'failed')

  @Override
  void testFailure(Failure failure) throws Exception {
    failure.description.addChild(FAILED)
  }

  @Override
  void testFinished(Description description) throws Exception {
    if (description.children.contains(FAILED))
      // Process failed tests here...
    else
      // Process passed tests here...
  }
}

Upvotes: 2

Related Questions