【发布时间】:2021-06-26 22:45:37
【问题描述】:
我正在使用new JUnit 5 concurrency feature 并行运行多个@SpringBootTest 类。它适用于小型测试集,但一旦我运行所有测试类,它将启动我的 Spring Boot 应用程序的 多个 实例。在日志中很容易看到(我使用的是JHipster)。
我很确定这不应该发生,因为我在 JUnit/Spring Boot 文档中没有找到任何关于这种行为的信息。
更奇怪的是,它似乎随机生成新实例。它以 2 开头,在执行过程中新的开始旋转(在服务器启动时暂停)。但是,它始终是相同的进程 ID,就好像实例在此过程中被销毁和重新创建...(请参阅下面的日志)
这是我的配置:
- spring-boot: 2.3.8.RELEASE
- junit 平台:1.7.1
- junit-jupiter: 5.7.1
- 在 Windows 10 和 IntelliJ IDEA 2020.2.4(社区版)上运行测试,但
mvn test会发生相同的行为
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
...
...
我希望有人遇到同样的问题。我怀疑这是一个配置问题,因为这里几乎所有东西都是开箱即用的......
提前感谢大家的时间!
【问题讨论】:
标签: java spring-boot concurrency junit5