简体   繁体   中英

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.

I'm using @SqlGroup and @Sql annotations, but I didn't discover how to indicate script files (sql) corresponding database.

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.

@Sql annotation lets you define a SqlConfig which contains a datasource bean name. Then you can define many datasource beans, with possibly different drivers and refer them from different @Sql. This might be helpful: 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

Then, somewhere in @Configuration class:

@Bean(name = "db1")
@ConfigurationProperties(prefix="spring.db1")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM