Reputation: 23
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.
Upvotes: 1
Views: 3080
Reputation: 896
@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();
}
Upvotes: 2