2

What I have: I'm developing a microservice, using Spring Boot with Web and SpringMyBatis. For CI integration tests I'll use a remote MySQL database, for local integration tests I'll use H2. I've got different application.yml file for both databases.

My problem: Since I want to create integration tests that will run on both databases, and I'll have to use different JDBC drivers (my tests will check directly on database what has been inserted/modified), what's the best way to do that? Could be a solution to use Spring JDBC to query my database, so that my datasource will be "picked" directly from application properties?

4
  • Ideally you need just one DataSource configured from a spring profile. Thus you can switch it on fly choosing necessary one for your CI. Commented Aug 23, 2017 at 12:56
  • Yes, that was my idea. So I need to configure my DataSource also on my @Config file and then retrieve the driver from that one? Commented Aug 23, 2017 at 13:15
  • Kind of. The only difference is Driver, connection URL and credentials. Commented Aug 23, 2017 at 13:20
  • Any idea on how to do that? I've got different connection URL and credentials on different application.yml. But haven't got any idea on how to do that... Commented Aug 23, 2017 at 13:21

2 Answers 2

2

As I mentioned it's good to use spring profile. Define 2 DataSources annotated with @Profile("dev") and @Profile("qa") and specify desired driver/connections url/credentials.

See the example (different datasources like you need) and/or the example to get more info

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, I got your answer. But since I'm actually using spring profiles to load different application properties, and on my application property I've already got all my driver data (url, user, pass..), Is there a way to recover THAT particular datasource?
It's possible but I don't know your configs and project structure. You have to change something in the code you didn't post
Ok, it works if I'll use the datasource this way: @Autowired DataSource datasource. It will pick the datasource set by the correct profile application.yml. Thanks for your hints!
Don't define multiple beans... Just add 2 different application-<proflile>.properties instead of multiple beans. That way spring boot will load the appropriate configuration file and configure the datasource. No need to make it more complex then that.
@M.Deinum in general I agree (+1) but there are cases when it's easier to define conditional bean than copy properties.
|
1

Spring Boot supports loading of different property files for different environments. Just create a proper application-<profile>.properties (or .yml and Spring Boot will load the appropriate one.

For instance in a application-qa.properties you specify the following

spring.datasource.url=jdbc:mysql:<remote-host>/db
spring.datasource.username=<username>
spring.datasource.password=<password>

If you want to use an embedded H2 for everything else you can omit configuration as when H2 is on the class path Spring Boot will auto configure an in-memory H2 for you.

Now when running the integration tests specify qa as the active profile and you will automatically be connected to the configured datasource. You can use Maven Profiles to select the active Spring Profile.

You don't need multiple DataSource beans in your configuration as Spring Boot will only configure the 1 requested.

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.