Search code examples
springjunitjunit5spring-testspring-context

Use JUnit 5 parallel test execution and still profit from Spring's context caching?


I am using Spring and JUnit 5.

In Spring tests, created contexts are cached so that they don't need to be re-created for each test.

However, according to the Spring documentation, this caching does not work when tests are executed in parallel:

Test suites and forked processes
The Spring TestContext framework stores application contexts in a static cache. This means that the context is literally stored in a static variable. In other words, if tests run in separate processes, the static cache is cleared between each test execution, which effectively disables the caching mechanism.

Is there a way, in which one can use JUnit 5's parallel test execution and still profit from Spring's context caching? Is there a parallel execution configuration that still works with Spring's context caching?


Solution

  • It seems that JUnit 5's parallel test execution works without problems with Spring's test context caching.

    For parallel test execution, JUnit 5 seems to use a ForkJoinPool under the hood. Multiple threads can access the statically cached Spring test contexts without a problem.