Overriding the default Runner.

Written on Monday, October 27th, 2008 at 11:10 am by David Saff
Filed under Uncategorized.

If you are running your JUnit 4.5 tests with a direct call to JUnitCore.run, you can override the default runner (BlockJUnit4ClassRunner) used to run tests that don’t otherwise have a @RunWith annotation:

RunnerBuilder builder = new AllDefaultPossibilitiesBuilder() { 
  @Override protected JUnit4Builder junit4Builder() {
    return new JUnit4Builder() {
      @Override public Runner runnerForClass(Class testClass) throws Throwable {
        return new MyCustomJUnit4ClassRunner(testClass);
      }
    };
  }
}

new JUnitCore().run(new Suite(AllMyTests.class, builder));

12 Responses to “Overriding the default Runner.”

  1. chronus Says:

    Cool!
    Is there a runner that allows stopping of tests while running already?

  2. David Saff Says:

    No, but it should be writable using the RunNotifier.pleaseStop() call.

  3. chronus Says:

    so if I want to write a plugin for an IDE that allows stopping of tests I must create a Runner? wasn’t there a way to stop them without implement a runner in JUnit 3?

  4. David Saff Says:

    Ah–a different question than I thought you were asking. If you’re writing an IDE plugin, then you are likely creating the RunListener yourself to pass to the JUnitCore, in which case you can call pleaseStop() on that listener whenever you need to, which is essentially the same flow as a JUnit 3 plugin.

  5. chronus Says:

    I only see pleaseStop() on RunNotifier, not on RunListener (http://junit.org/apidocs/org/junit/runner/notification/RunListener.html)

    am I missing something? =)

  6. idan shay Says:

    Hi David,

    MyCustomJUnit4ClassRunner is custom version for BlockJUnit4ClassRunner ?

    Where do I add the code that will call @BeforeClass listener ?

  7. David Saff Says:

    Please look at the implementation of BlockJUnit4ClassRunner and its parent classes, at where RunNotifier is notified. Feel free to follow up on junit@yahoogroups.com

  8. idan shay Says:

    Hi David,

    I extend the BlockJUnit4ClassRunner to myCustomRunner that override withBeforeClasses to use MyRunBefores instead of RunBefores so I have the control before and after the @BeforeClass start.

    The problem is that I don’t have th list of listeners that are save as private fiels at RunNotifier that is private field at JUnitCore.

    Is it possible to set them as protected or add getters to this fields ?

    Because there are private I cannot extend them.

    Thanks

  9. David Saff Says:

    Even if you had the list of listeners, there’s no fireTestBeforeClass, so you’d be out of luck there. I recommend you maintain your own list of listeners that is interested in the BeforeClass event.

  10. idan shay Says:

    Hi David,

    I thought to keep the list o listeners as you also suggest and this is what I probably do.
    I think that for next version it will good if for example the class RunNotifier could be extended or/and have get/set access methods to the fields.
    This will make junit more flexable like you did for the runner.

    I can extend the listener to support BeforCalssStart but as you said I cant do that to the runNotifier.
    Even if I override the run method in my runner to get the notifer value I still not able to access the list of listeners so I can call them from MyRunBefores.

    If one of the solution I mention to get the list of listener (like add getter in RunNotifer for this field) could be added as Patch it will be great.

    Thanks
    Idan

  11. Greg Says:

    Do you know if there is a way to override the default runner when running junit from ant?

  12. David Saff Says:

    Not currently. Feature requests are appreciated

Leave a Reply

You must be logged in to post a comment.