[英]Spring Boot integration test using multiple database types
In my test I need test with different databases (mysql, oracle, etc.) and I would like to know if it's possible with SpringRunner. 在我的测试中,我需要对不同的数据库(mysql,oracle等)进行测试,我想知道SpringRunner是否可能。
I'm using @SqlGroup and @Sql annotations, but I didn't discover how to indicate script files (sql) corresponding database. 我正在使用@SqlGroup和@Sql批注,但是我没有发现如何指示脚本文件(sql)对应的数据库。
Example: 例:
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:tenantBeforeTestRun.sql")
This annotation configures my test to execute the script to all database types, but this file didn't work on Oracle. 此注释将我的测试配置为对所有数据库类型执行脚本,但是该文件在Oracle上不起作用。
@Sql
annotation lets you define a SqlConfig
which contains a datasource
bean name. @Sql
注释使您可以定义一个SqlConfig
,其中包含datasource
bean名称。 Then you can define many datasource beans, with possibly different drivers and refer them from different @Sql. 然后,您可以定义许多具有不同驱动程序的数据源bean,并从不同的@Sql引用它们。 This might be helpful: Spring Boot Multiple Datasource
这可能会有所帮助: Spring Boot Multiple Datasource
@Sql(..., config = @SqlConfig(datasource = "db1", ...)
application.properties: 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
Then, somewhere in @Configuration
class: 然后,在
@Configuration
类中的某个位置:
@Bean(name = "db1")
@ConfigurationProperties(prefix="spring.db1")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.