使用 JUnit 5 的并发 Spring 启动测试正在启动多个 JVM

Concurrent Spring Boot tests using JUnit 5 are starting mulitple JVMs

我正在使用 new JUnit 5 concurrency feature to run multiples @SpringBootTest classes in parallel. It works great with a small test set but once I run all the tests classes, it's going to start multiple instances of my Spring Boot app. It's pretty easy to see in the logs (I'm using JHipster).

我很确定这不应该发生,因为我在 JUnit/Spring 引导文档中没有找到任何关于此行为的信息。

更奇怪的是它似乎随机生成新实例。它从 2 开始,新的在执行过程中旋转起来(在服务器启动时暂停)。但是,它始终是 相同的 进程 ID,就好像实例在此过程中被销毁并重新创建一样...(请参阅下面的日志)

这是我的配置:

junit-platform.properties配置:

junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4

所有测试 类 都带有以下注释:

@Import({ PostgresqlTestConfig.class, MailTestConfig.class, ServerContext.class, CaptchaTestConfig.class })
@SpringBootTest(classes = { MyApp.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("prod")

在日志中,我可以清楚地看到 SB 应用启动程序多次:


        ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
        ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
        ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝
  ██╗   ██║ ██╔═══██║    ██║    ██╔════╝   ╚═══██╗    ██║    ██╔═══╝   ██╔══██║
  ╚██████╔╝ ██║   ██║ ████████╗ ██║       ██████╔╝    ██║    ████████╗ ██║  ╚██╗
   ╚═════╝  ╚═╝   ╚═╝ ╚═══════╝ ╚═╝       ╚═════╝     ╚═╝    ╚═══════╝ ╚═╝   ╚═╝

:: JHipster   :: Running Spring Boot 2.3.8.RELEASE ::
:: http://jhipster.github.io ::

2021-03-30 23:47:08.593  INFO 7200 --- [Pool-1-worker-5] .w.w.w.r.a.JwtAuthenticationResourceTest : Starting JwtAuthenticationResourceTest on MSI with PID 7200 
...
... (initializing the context here, i.e embedded PostgreSQL, running Liquibase, etc...)
...
2021-03-30 23:48:54.811  INFO 7200 --- [Pool-1-worker-5] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_MSI1617140910513 started.
2021-03-30 23:48:54.850  INFO 7200 --- [Pool-1-worker-5] .w.w.w.r.a.JwtAuthenticationResourceTest : Started JwtAuthenticationResourceTest in 106.911 seconds (JVM running for 118.405)

... Then a bit later ...


        ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
        ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
        ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝
  ██╗   ██║ ██╔═══██║    ██║    ██╔════╝   ╚═══██╗    ██║    ██╔═══╝   ██╔══██║
  ╚██████╔╝ ██║   ██║ ████████╗ ██║       ██████╔╝    ██║    ████████╗ ██║  ╚██╗
   ╚═════╝  ╚═╝   ╚═╝ ╚═══════╝ ╚═╝       ╚═════╝     ╚═╝    ╚═══════╝ ╚═╝   ╚═╝

:: JHipster   :: Running Spring Boot 2.3.8.RELEASE ::
:: http://jhipster.github.io ::

2021-03-30 23:51:14.626  INFO 7200 --- [Pool-1-worker-3] c.w.w.security.AuthenticationFlowTest    : Starting AuthenticationFlowTest on MSI with PID 7200
...
...

我希望有人遇到同样的问题。我怀疑这是一个配置问题,因为这里几乎所有东西都是现成的...

提前感谢大家的宝贵时间!

经过漫长而繁琐的反复试验,我想通了,很简单。

它与 JUnit 5 或并行执行没有任何关系,而且在我在项目中使用它之前就已经发生了。它与 Spring 在某些情况下不重用应用程序上下文 的启动测试有关。

那里有更多详细信息:

一旦我的所有测试 类 使用完全相同的上下文配置,只有一个 server/context 被加载并且并行执行运行良好!