簡體   English   中英

Spring 為每個單元測試文件加載上下文

[英]Spring loads context for each unit test file

我有 16 個單元測試用例文件。 每個格式如下:

@SpringBootTest
class UserServiceTest {

  @Autowired
  UserService userService;

  @MockBean
  SomeDependency someDependency;

  @Test
  ...and so on

}

每當我運行mvn clean install ,spring 似乎都在重新啟動/加載上下文 16 次。 我看到這些日志 16 次:

[INFO] Running com.base.UserServiceTest
2021-07-16 04:35:39.421 [INFO ] [main] o.s.t.c.s.AbstractTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.base.UserServiceTest], using SpringBootContextLoader
2021-07-16 04:35:39.423 [INFO ] [main] o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.base.UserServiceTest]: no resource found for suffixes {-context.xml, Context.groovy}.
2021-07-16 04:35:39.424 [INFO ] [main] o.s.t.c.s.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.base.UserServiceTest]: UserServiceTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2021-07-16 04:35:39.473 [INFO ] [main] o.s.b.t.c.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.base.Application for test class com.base.UserServiceTest
2021-07-16 04:35:39.475 [INFO ] [main] o.s.t.c.s.AbstractTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2021-07-16 04:35:39.476 [INFO ] [main] o.s.t.c.s.AbstractTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@4dc599a7, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@635f2d9b, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@7e83380f, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@6000fc20, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1e95926, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1bc08f75, org.springframework.test.context.transaction.TransactionalTestExecutionListener@7ce49f42, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1b629d02, org.springframework.test.context.event.EventPublishingTestExecutionListener@5089c721, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@4fad5162, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@176bd95a, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@186edc38, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@6c0b0db, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@2343c720, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@6aab6b1b]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

2021-07-16 04:35:39.520 [INFO ] [main] o.s.b.StartupInfoLogger - Starting UserServiceTest using Java 11.0.2 on 
2021-07-16 04:35:39.523 [INFO ] [main] o.s.b.SpringApplication - The following profiles are active: local
2021-07-16 04:35:40.197 [INFO ] [main] o.s.m.w.MockServletContext - Initializing Spring TestDispatcherServlet ''
2021-07-16 04:35:40.197 [INFO ] [main] o.s.w.s.FrameworkServlet - Initializing Servlet ''
2021-07-16 04:35:40.199 [INFO ] [main] o.s.w.s.FrameworkServlet - Completed initialization in 1 ms
2021-07-16 04:35:40.211 [INFO ] [main] o.s.b.StartupInfoLogger - Started UserServiceTest in 0.732 seconds (JVM running for 9.613)
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.918 s - in com.base.UserServiceTest

這會導致兩個問題:

  1. 還有很多測試要添加。 即使有 16 個測試文件,執行也需要大約 20 秒。 所以這個過程很慢。
  2. 我的應用程序使用 JPA 連接數據庫,並且在mvn clean install期間,雖然應用程序構建成功,但它在日志中隨機出現此錯誤:

2021-07-16 04:35:52.741 [ERROR] [main] czhpHikariPool - HikariPool-11 - Exception during pool initialization. org.postgresql.util.PSQLException: FATAL: remaining connection slots are reserved for non-replication superuser connections

我想知道這種上下文重新加載是否會導致這種情況,因為我經常執行mvn clean install並且某處可能存在連接泄漏。

使用 JUnit 5/Jupiter 和 Spring 5。

任何幫助解決以上 2 點的問題表示贊賞。 謝謝。

如果上下文配置合適,Spring Test 會緩存您的 TestContext並將其重新用於另一個測試。

但是,如果您所有的測試都有不同的設置(例如,第一個測試需要類 A 的模擬版本,而第二個測試需要類 B 的模擬版本),緩存將無濟於事,Spring 必須啟動一個新的上下文。

如果您簡化所有測試的上下文設置,Spring 將重用它,從而加快測試執行速度。

此外,您添加的測試(使用@SpringBootTest不是單元測試(好吧 - 它始終取決於定義),因為它啟動了整個 Spring 上下文。

如果您的計划是為您的業務邏輯編寫單元測試,請只使用 JUnit 5 和 Mockito。 這些測試很快,並且沒有 Spring 支持,因此沒有啟動上下文。

有關 Spring Boot 應用程序的單元、集成和端到端測試策略的廣泛概述,請查看本文

暫無
暫無

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

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