簡體   English   中英

並發 Spring 使用 JUnit 5 的引導測試正在啟動多個 JVM

[英]Concurrent Spring Boot tests using JUnit 5 are starting mulitple JVMs

我正在使用新的 JUnit 5 並發功能並行運行多個@SpringBootTest類。 它適用於小型測試集,但一旦我運行所有測試類,它將啟動我的 Spring 引導應用程序的多個實例。 在日志中很容易看到(我正在使用JHipster )。

我很確定這不應該發生,因為我在 JUnit/Spring Boot 文檔中沒有找到任何關於這種行為的信息。

更奇怪的是,它似乎隨機產生了新的實例。 它以 2 開頭,在執行過程中新的開始旋轉(在服務器啟動時暫停)。 但是,它始終是相同的進程 ID,就好像實例在此過程中被銷毀和重新創建一樣......(請參閱下面的日志)

這是我的配置:

  • spring-boot:2.3.8.RELEASE
  • junit平台:1.7.1
  • junit-木星: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
...
...

我希望有人遇到同樣的問題。 我懷疑這是一個配置問題,因為這里幾乎所有東西都是開箱即用的......

提前感謝大家的時間!

經過漫長而乏味的反復試驗過程,我想通了,這很簡單。

它與 JUnit 5 或並行執行沒有任何關系,並且在我在項目中使用它之前就已經發生了。 它與Spring 在某些情況下不重用應用程序上下文的引導測試有關。

那里的更多細節:

一旦我的所有測試類都使用完全相同的上下文配置,只加載一個服務器/上下文並且並行執行運行良好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM