Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support registration of global extensions via ServiceLoader mechanism #448

Closed
reinhapa opened this issue Aug 5, 2016 · 13 comments
Closed

Comments

@reinhapa
Copy link
Contributor

reinhapa commented Aug 5, 2016

Overview

Since we have over 2000 tests that we would have to annotate with @ExtendWith, it would be nice to just add also extensions using the META-INF/service/org.junit.jupiter.api.extension.Extension mechanism (later under JDK 9 via service within module-info).

Is there a good reason to not have this possibility?

Pull Requests

@reinhapa reinhapa changed the title Adding Extensions via java.util.ServiceLoader possible? Adding additional global Extensions using java.util.ServiceLoader Aug 6, 2016
@sbrannen sbrannen changed the title Adding additional global Extensions using java.util.ServiceLoader Support registration of global Extensions via java.util.ServiceLoader Aug 8, 2016
@marcphilipp
Copy link
Member

There is a good reason: Tests should be easy to understand.

However, I feel your pain. Maybe we could make this an opt-in feature that can be turned on via a configuration parameter / system property.

@junit-team/junit-lambda What do you think?

@reinhapa
Copy link
Contributor Author

Hmm, I see you're point there. How would a opt-in feature look like?

@marcphilipp
Copy link
Member

You could, e.g., enable global extensions via a system property. That would give you at least an indication...

@bechte
Copy link
Contributor

bechte commented Sep 13, 2016

I still feel that we should not go in this direction:

  • JUnit5 supports JUnit4 tests and test need not to be changed to be executed on the new platform. Therefore, a migration is simple.
  • If the tests are migrated to the JUnit5 Jupiter programming model, then at least the @Test annotation needs to be migrated to the Jupiter version. Using meta-annotation it would be easy to define a project specific version of @Test that adds the required @ExtendWith meta-annotations and, therefore, activates the default extension(s) on the fly.

While adding (very) more complexity to the extension registration, I cannot see the huge advantages that shall come along with this proposal. Even worse, it makes it more complex as we might also have to add a mechanism to "unregister" a globally registered extension for certain test cases. This is not worth the effort.

@schauder
Copy link
Contributor

I very often wished there was a feature like this. Most often the scenario was like this:

  • I create a test that works just fine on its own
  • I run it with the complete test suite with a gazillion other tests and my new test fails.
  • I realize one of the other tests is missing a teardown/after logik and leave some dirty state behind it.

What I ended up doing is: manually splitting all test in two groups, run them, check if my new test fails. If yes, split the tests in two again ... until finally I identify one bad test. Rinse and repeat until all bad apples are found.

With a global extention registry I could write a service that checks for left over state after each test and get a list of tests that need fixing.

Therefore I vote for such a feature.

@sbrannen
Copy link
Member

However, I feel your pain. Maybe we could make this an opt-in feature that can be turned on via a configuration parameter / system property.

@junit-team/junit-lambda What do you think?

The Spring TestContext Framework supports this for the TestExecutionListener and ContextCustomizerFactory APIs, and this can be extremely useful.

For example, Spring Boot and Spring Security can auto-magically contribute custom testing features without requiring the user to do anything other than add the spring-boot-test or spring-security-test JAR to the classpath.

But that automatic registration itself is only part of the picture. The more important piece of the puzzle is that users can upgrade to a new version of Spring Security Test and benefit from its new testing features (provided via extensions/listeners) without having to configure every single test class in the entire test suite to register the new Spring Security Test feature alongside the existing configuration for the previous version of Spring Security Test.

Thus, I am in favor of supporting this feature in general and at the very least as an opt-in only feature.

@sbrannen
Copy link
Member

@junit-team/junit-lambda, and objections on pulling this in before 5.0 GA?

@sbrannen sbrannen added this to the 5.0 M5 milestone Mar 15, 2017
@sbrannen
Copy link
Member

Tentatively slated for M5.

@reinhapa
Copy link
Contributor Author

@sbrannen where would be the correct place to add the according documentation to?

@sbrannen
Copy link
Member

In the Registering Extensions section of the User Guide.

@reinhapa
Copy link
Contributor Author

@sbrannen I've updated my pull request with some documentation

@sbrannen sbrannen self-assigned this Mar 24, 2017
@sbrannen sbrannen changed the title Support registration of global Extensions via java.util.ServiceLoader Support registration of global extensions via ServiceLoader mechanism Mar 25, 2017
@sbrannen sbrannen modified the milestones: 5.0 M4, 5.0 M5 Mar 25, 2017
@sbrannen
Copy link
Member

Now slated for M4.

sbrannen pushed a commit that referenced this issue Mar 25, 2017
Prior to this commit, custom extensions could only be registered
declaratively via @ExtendWith.

This commit introduces support for auto-detection of extensions
via Java's ServiceLoader mechanism. Such extensions will be
automatically registered after the default global extensions
in JUnit Jupiter (e.g., support for TestInfo, TestReporter, etc.).

Furthermore, this feature is disabled by default but can be enabled
via a JVM system property or JUnit Platform configuration parameter.
Consult the JavaDoc and User Guide for details.

Issue: #448
sbrannen added a commit that referenced this issue Mar 25, 2017
@sbrannen
Copy link
Member

Resolved in 473aa1a.

sbrannen added a commit that referenced this issue Mar 25, 2017
In addition, default extensions are now logged at TRACE level.

Issue: #448
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

5 participants