Skip to content

Use LauncherInterceptor for JUnit tests#4226

Merged
aromaa merged 4 commits intoapi-14from
api14/unit-tests-launch-interceptor
Sep 10, 2025
Merged

Use LauncherInterceptor for JUnit tests#4226
aromaa merged 4 commits intoapi-14from
api14/unit-tests-launch-interceptor

Conversation

@aromaa
Copy link
Member

@aromaa aromaa commented Sep 9, 2025

We are now guaranteed to be in the transforming class loader during unit test loading and their execution, ensuring class loading happens from the correct ClassLoader.

}
final ModuleFinder spongeFinder = this.createModuleFinder(spongeJars);
final ModuleFinder resourceFinder = this.createModuleFinder(resourceJars);
parentLoader = new FilteringPassthroughClassLoader(contextLoader,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that the classes that are supposed to be inside the module layers are not loaded through the bootstrap layer as we are now exposing a hole that can access the application class loader and thus gain access to the original classpath. Classes that we don't end up loading inside the module layers are still accessible and fallback to the parent.

urls[i] = resourcePaths.get(i).toUri().toURL();
}
parentLoader = new URLClassLoader("BOOTSTRAP-RESOURCES", urls, parentLoader);
parentLoader = new ResourceClassLoader("BOOTSTRAP-RESOURCES", urls, parentLoader);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this could cause class loading from the resources we just provided outside of the module layers.


@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rejects all class loading that failed to be fetched from the parent.

@aromaa aromaa requested a review from Yeregorix September 9, 2025 19:38
@Yeregorix
Copy link
Member

Yeregorix commented Sep 10, 2025

LGTM!
We expose the same JUnit services in SV production so that any plugin dev can easily setup unit tests. So I just copied your changes there.

See https://github.com/Yeregorix/AutoPickup/blob/feature/unit-tests/build.gradle#L71 for a POC. In the long term, I plan to pre-configure everything in SpongeGradle.

@aromaa aromaa merged commit 6820483 into api-14 Sep 10, 2025
13 checks passed
@aromaa aromaa deleted the api14/unit-tests-launch-interceptor branch September 10, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants