question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add SPI support (Service Provider Interface, from Java 6) to enable external RunListener

See original GitHub issue

The @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:open
  • Created 14 years ago
  • Comments:45 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
kriegaexcommented, Jul 9, 2020

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 because RunWith 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.

0reactions
kcooneycommented, May 29, 2014

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Service Provider Interface | Baeldung
Java 6 has introduced a feature for discovering and loading implementations matching a given interface: Service Provider Interface (SPI).
Read more >
Creating Extensible Applications (The Java™ Tutorials > The ...
Implements the SPI. An application with extensible services enable you, vendors, and customers to add service providers without modifying the original ...
Read more >
Chapter 6. Service Provider Interfaces (SPI)
It allows you to pull in external dependencies among other fine grain actions. ProviderFactory implementations are required to be plain java objects. But,...
Read more >
Implementing Plugins with Java's Service Provider Interface
Then we will give an implementation of an SPI for a practical use case. Example Code. This article is accompanied by a working...
Read more >
Looking into java service provider interface (SPI ... - YouTube
I took a look at the service provider interface in java. This is a needed interface to build simple services using easy building...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found