使用 JUnit 5 并行测试执行并仍然从 Spring 的上下文缓存中获益?

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

我正在使用 Spring 和 JUnit 5

In Spring tests, created contexts are cached 这样就不需要为每个测试重新创建它们。

但是,根据 Spring 文档,此缓存在并行执行测试时起作用:

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.

有没有一种方法可以使用 JUnit 5's parallel test execution 并仍然从 Spring 的上下文缓存中获益?是否有并行执行配置仍然适用于 Spring 的上下文缓存?

似乎 JUnit 5 的并行测试执行在 Spring 的测试上下文缓存中没有问题。

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

是的,您可以使用 JUnit Jupiter(JUnit 5)对并行测试执行的支持以及 Spring 的集成测试支持。

但是,您要避免的主要事情是使用 @DirtiesContext@MockBean@SpyBean

可以在 Spring 框架参考手册的 Parallel Test Execution 部分找到详细信息。