Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

In the deprecated org.junit.platform.runner.JUnitPlatform, one could easily write the following

@RunWith(CustomRunner.class)
public MyTest {
  // ...
}

with

public class CustomRunner extends JUnitPlatform {
  public CustomRunner(final Class<?> clazz)
      throws NoSuchFieldException, SecurityException, IllegalArgumentException,  IllegalAccessException {
    super(shadowTestClass(clazz));
  }
  // ...
  private static Class shadowTestClass(Class testClass) {
    return modified test class, poissibly with custom classloader;
  }
}

I'm not quite sure it's possible to do the same using current JUnit feature set. From I found, in particular in ClassSelectorResolver the class is loaded then immediately passed to the ClassTestDescriptor which is then propagated along the way.

I've also seen this selector org.junit.platform.engine.discovery.DiscoverySelectors#selectClass(java.lang.ClassLoader, java.lang.String) but I'm not sure how that fits in the JUnit 5 extensions at this point.

You must be logged in to vote

As a side note, if you're interested in support for a custom ClassLoader, you really should search the open issues for discussions related to "ClassLoader" before starting a discussion.

Replies: 2 comments · 4 replies

Comment options

This sounds like a job for a custom test instance factory: https://docs.junit.org/current/user-guide/#extensions-test-instance-factories

Common use cases include acquiring the test instance from a dependency injection framework or invoking a static factory method to create the test class instance.

You must be logged in to vote
2 replies
@bric3
Comment options

I'll look into this avenue. Thanks a lot.

I wonder if the ctx.getRequiredTestClass() in extensions method like public void beforeAll(ExtensionContext ctx) can see the modified class though.

Indeed after exploring selectors it appears a bit too early in the process, it even appears in gradle itself.

@bric3
Comment options

Hey didn't had a chance to go back to you, thanks for the suggestion it's very useful ! However after some discussions with teammates, it's probably better to not go that path if there are other ways.
Thank you !

Comment options

To support your use case, you may be able to register a custom LauncherInterceptor. See the example in the User Guide for details.

Related Issues

You must be logged in to vote
2 replies
@sbrannen
Comment options

sbrannen Sep 4, 2025
Collaborator

As a side note, if you're interested in support for a custom ClassLoader, you really should search the open issues for discussions related to "ClassLoader" before starting a discussion.

Answer selected by bric3
@bric3
Comment options

Thanks for the feedback I was initially interested to load a "customized" test class. That's why I didn't found anything that might apply with the other classloader discussions.

However, after some sleep, we may want to look at something more regular and drop the existing approach that patch the test class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.