Situation:
Spring Cloud
with Spring Boot
in a microservice, that microservice is loading a DB config information to config
There is an option to fake Spring bean with just plain Spring features. You need to use @Primary
, @Profile
and @ActiveProfiles
annotations for it.
I wrote a blog post on the topic.
You can use in memory DB (e.g. H2) to replace real data source. Something like this:
@Configuration
public class TestingDataSourceConfig {
@Bean
@Primary
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(H2)
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.addScript("schema.sql")
.addScripts("user_data.sql", "country_data.sql")
.build();
}
}