1

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.

1
  • You probably need to provide your configuration and Spring boot application to be able to know what is the problem. Commented May 8, 2018 at 19:08

1 Answer 1

2

@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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.