簡體   English   中英

Spring-boot 1.4中的新@DataJpaTest以及與application.properties的關系

[英]New @DataJpaTest in spring-boot 1.4 and relation with application.properties

我正在使用新的Spring Boot 1.4功能更新代碼,並在集成測試中嘗試使用新的@DataJpaTest批注。 我要運行的.sql文件有問題,其中一些僅用於集成測試,另一種用於將來的生產數據庫。

在集成測試中,我使用@Sql批注,並將.sql文件放在/src/test/resources目錄和H2內存數據庫中。 為了生產,我使用的是mysql數據庫,並且在/src/main/resources有一個data-mysql.sql文件。

當我運行集成測試時,我希望只看到/src/test/resources內部的文件正在執行,但是我的data-mysql.sql也正在執行。

這是正常行為嗎? 有一個簡單的改變嗎​​?

樣例代碼:

@RunWith(SpringRunner.class)
@DataJpaTest
@Sql("/test-data.sql")
public class CdrIntegrationTest {
     // any tests here
}

我的“生產”配置是這些配置:

spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.datasource.url=jdbc:mysql://localhost/prod
spring.datasource.username=root
spring.datasource.password=***
spring.datasource.initialize=true
spring.datasource.platform=mysql

謝謝你的幫助。

通過“生產”配置,它們只是在application.properties中嗎? 默認情況下,使用SpringRunner和@DataJpaTest運行測試時,它將仍然引導加載那些application.properties的ApplicationContext,因此嘗試在測試期間加載data- {platform} .sql文件。 本文檔描述了要檢查的屬性的順序: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html #1正在使用@TestPropertySource。 這樣,您可以在src / test / resources中創建一個h2-test.properties文件,並為其定義數據源屬性,尤其要注意spring.datasource.platform。

暫無
暫無

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

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