I have been trying to set up a Junit 5 extension to force every test to get a separate ClassLoader. I am able to do it quite easily in Junit4, creating my own BlockJUnit4ClassRunner. But, I fail to have it work now.
The purpose is to be able to test things such as static blocks or memorized fields in different states.
I have been trying to use the TestInstanceFactory without any success so far with something like that:
public class SeparateClassLoaderExtension implements TestInstanceFactory {
@SneakyThrows
@Override
public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException {
ClassLoader testClassLoader = new TestClassLoader();
final Class<?> testClass = Class.forName(factoryContext.getTestClass().getName(), true, testClassLoader);
Constructor<?> defaultConstructor = testClass.getDeclaredConstructor();
defaultConstructor.setAccessible(true);
return defaultConstructor.newInstance();
}
}
I get an exception from Junit saying that the class is not of the right type.
Someone any idea?
JUnit Jupiter does not support this, yet. Here's the related issue: https://github.com/junit-team/junit5/issues/201
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With