[英]Spring Boot integration test using multiple database types
在我的測試中,我需要對不同的數據庫(mysql,oracle等)進行測試,我想知道SpringRunner是否可能。
我正在使用@SqlGroup和@Sql批注,但是我沒有發現如何指示腳本文件(sql)對應的數據庫。
例:
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:tenantBeforeTestRun.sql")
此注釋將我的測試配置為對所有數據庫類型執行腳本,但是該文件在Oracle上不起作用。
@Sql
注釋使您可以定義一個SqlConfig
,其中包含datasource
bean名稱。 然后,您可以定義許多具有不同驅動程序的數據源bean,並從不同的@Sql引用它們。 這可能會有所幫助: Spring Boot Multiple Datasource
@Sql(..., config = @SqlConfig(datasource = "db1", ...)
application.properties:
#first db
spring.db1.url = [url]
spring.db1.username = [username]
spring.db1.password = [password]
spring.db1.driverClassName = oracle.jdbc.OracleDriver
#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver
然后,在@Configuration
類中的某個位置:
@Bean(name = "db1")
@ConfigurationProperties(prefix="spring.db1")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.