I am trying to run JUnit 5 tests in parallel using Spring framework.
I have a singleton bean: WebDriver that needs to be injected in about 4 places(POJO classes). Then a test-class will inject some of these POJO classes to run the test methods. Because other tests will run following the same model, with the same bean(webdriver), when a test ends, the bean session ends, and the other parallel test running fail bc the bean has been destroyed.
how could I make the singleton webdriver be shared per test method, (and not across the test suit execution)so there is a different webdriver bean per every tests session?
I created an extension that tries to do that:
public void interceptTestMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable
{
ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
poolTaskExecutor.execute(()->
{
try
{
invocation.proceed();
}
catch (Throwable throwable)
{
throwable.printStackTrace();
}
});
poolTaskExecutor.shutdown();
poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
}
This will put every tests in a threadpool making the test thread safe (in theory). But the WebDriver bean is still shared among the other threads for other tests methods.
I tried to make webdriver prototype, but that creates too many beans when running 6 tests at a time, that the test suit crashes.