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

Viewed 253

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?

2 Answers

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