0

I have implemented my own test runner with an overridden runChild() method:

public class MyTestRunner extends BlockJUnit4ClassRunner {

  // ...

  @Override
  protected void runChild(FrameworkMethod method, RunNotifier notifier) {
    if (method.getAnnotation(Ignore.class) != null) {
        return;
    }

    // Do some global pre-action
    // ...

    // Runs the passed test case
    super.runChild(method, notifier);

    // Do some global post-action depending on the success of the test case
    // ...
  }

  // ...

}

I override this method because I need to do some global pre- and post-actions before/after the test case execution. The post-action will be dependent on the failure/success of the test case execution. How can I retrieve the execution result?

user1613270
  • 523
  • 10
  • 20

1 Answers1

0

I've found a solution by registering a listener before executing runChild():

        // ...

        // Add callback listener to do something on test case success
        notifier.addListener(new RunListener() {
            @Override
            public void testRunFinished(Result result) throws Exception {
                super.testRunFinished(result);
                if (result.getFailureCount() == 0) {
                    // Do something here ...
                }
            }
        });

        // Runs the passed test case
        super.runChild(method, notifier);

        // ...

But is there a better way to do it?

user1613270
  • 523
  • 10
  • 20
  • Rules must be implemented on test class/method level, but I don't want my tests to know anything about the post-actions – user1613270 Nov 25 '13 at 08:30