-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LauncherInterceptor
does not replace the ClassLoader
for all tests
#3392
Comments
LauncherInterceptor
does not replace the ClassLoader
for all tests
I think we might be able to close this. @stuartwdouglas has pointed out that my test could never pass, because the child classloader has no resources attached to it and has no custom behaviour, so it would always delegate to the parent. That means the reported classloader would always be the parent classloader. With this revised interceptor constructor, the classloader gets to do more, and my test does pass: public CustomLauncherInterceptor() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
customClassLoader = new ClassLoader("SuperCustom", parent) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, true);
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
var ex = findLoadedClass(name);
if (ex != null) {
return ex;
}
var res = getResource(name.replace(".", "/") + ".class");
if (res != null && res.getProtocol().equals("file")) {
try (var in = res.openStream()) {
var data = in.readAllBytes();
return defineClass(name, data, 0, data.length);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return super.loadClass(name, resolve);
}
};
} Apologies for the noise! |
Hi Holly, Thanks for the feedback, and we're glad to know you sorted it out! Cheers, Sam |
just out of curiosity has anyone managed to get this working for tests within a WAR file? See my stackoverflow post |
I'm trying out JUnit 5.10-RC1. The description of the new LauncherInterceptor API on #201 is
I'm finding that the interceptor gets called at an early phase in the lifecycle, but setting the TCCL does not seem to affect what classloader is used for test class loading. Is there an extra step I'm missing?
Steps to reproduce
I'm attaching a simple reproducer. It's very similar to the documented example. I make a launcher interceptor which sets a new classloader on the thread context classloader. I can see, from printlns, that the interceptor is being called.
In my test, I check what classloader the test class was loaded with:
The test fails:
Resources
Context
The text was updated successfully, but these errors were encountered: