Add SPI support (Service Provider Interface, from Java 6) to enable external RunListener
See original GitHub issueThe @RunWith annotation has been widely successful in enabling the integration of other testing tools with JUnit. For example, the following testing libraries (among others) use it: jMock, Mockito, JMockit, PowerMock, Unitils, Spring Test.
However, this mechanism requires the annotation be applied to each and every test class, and does not support the use of more than one custom runner for the same test class.
I would like to propose a simple enhancement to JUnit’s ability to integrate with other testing tools: add support for the automatic discovery of org.junit.runner.notification.RunListener implementations, through the standard Service Provider API available in Java SE 6 (the java.util.ServiceLoader class).
I am the creator of the JMockit testing toolking (http://code.google.com/p/jmockit), which already implements sophisticated integration with the JUnit test runner. However, to provide the best user experience I need JMockit to get initialized before JUnit starts running the first test. I have achieved this, but not in a clean way. The mechanism I propose would provide such a clean solution, which I believe might also be useful to other testing libraries which integrate with JUnit.
The desired SPI support can be achieved with the addition of the following four lines of code to the constructor of org.junit.runner.JUnitCore (alternatively, it could be done inside the RunNotifier class):
if ("1.6 1.7".contains(System.getProperty("java.specification.version"))) {
ServiceLoader<RunListener> serviceLoader = ServiceLoader.load(RunListener.class);
for (RunListener listener : serviceLoader)
fNotifier.addListener(listener);
}
The code above requires Java 6, but it would not interfere with test suites compiled for and running under Java 5 (or older).
Thanks
Issue Analytics
- State:
- Created 14 years ago
- Comments:45 (24 by maintainers)
Top GitHub Comments
Just found this old ticket by chance and can only say how appalled I am to find out for how long such a simple, universally useful feature which is now marketed as a great achievement in JUnit 5 has not just been ignored but actively blocked with poor pseudo arguments by people who should know better.
Given the still big user base of JUnit 4, I would still be happy to see this backported into 4.13 (or 4.14 then) before really freezing development.
Sorry for being opinionated, but I have reasons for my opinion. I wrote a little test tool which I could integrate easily with JUnit 5, Spock and TestNG via two lines of code each, utilising SPI in all cases. For JUnit 4 I had to implement a runner and burden each user with annotating each test by
@RunWith
plus extra configuration which runner to delegate to becauseRunWith
does not even accept extra parameters to hand over config parameters, say a string array, and there is no concept of chaining runners. Rules don’t cut it here for bootstrapping reasons.@dsaff I think it’s unlikely that any current result formatters would change to indicate that the plug-in was available unless we made the changes to their formatters.
@baev someone in the thread suggested that not all code that runs JUnit tests would use JUnitCore. So we would probably need to install it in RunNotifier.