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

Time:03-26

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?

CodePudding user response:

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.

CodePudding user response:

Yes, you can use JUnit Jupiter's (JUnit 5's) support for parallel test execution with Spring's integration testing support.

The main thing you'll want to avoid, however, is the use of @DirtiesContext, @MockBean, or @SpyBean.

Details can be found in Parallel Test Execution section of the Spring Framework reference manual.

  • Related