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));
October 27th, 2008 at 12:42 pm
Cool!
Is there a runner that allows stopping of tests while running already?
October 27th, 2008 at 12:47 pm
No, but it should be writable using the RunNotifier.pleaseStop() call.
October 27th, 2008 at 1:17 pm
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?
October 27th, 2008 at 1:19 pm
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.
October 27th, 2008 at 1:21 pm
I only see pleaseStop() on RunNotifier, not on RunListener (http://junit.org/apidocs/org/junit/runner/notification/RunListener.html)
am I missing something? =)
October 27th, 2008 at 2:19 pm
Hi David,
MyCustomJUnit4ClassRunner is custom version for BlockJUnit4ClassRunner ?
Where do I add the code that will call @BeforeClass listener ?
October 27th, 2008 at 2:26 pm
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
October 30th, 2008 at 4:57 pm
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
October 31st, 2008 at 10:25 am
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.
October 31st, 2008 at 4:04 pm
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
February 20th, 2009 at 11:16 am
Do you know if there is a way to override the default runner when running junit from ant?
April 9th, 2009 at 9:43 am
Not currently. Feature requests are appreciated