0

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.

Jethro Axel
  • 23
  • 1
  • 4
  • I wonder if there is a way to autowire the bean inside the lambda expression, or have an extension that creates the test and then, wire the bean from the bean factory at the time the tests are created. – Jethro Axel May 05 '20 at 22:12
  • I used https://stackoverflow.com/questions/52457293/selenium-driver-management-in-springboot/52540842 To make Spring and JUnit Jupiter working in parallel – Jethro Axel May 20 '20 at 20:53

0 Answers0