With spring boot + spring data jdbc i have to wire the DataSource bean by myself. Like so:
@Bean
public DataSource dataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(this.environment.getRequiredProperty("url"));
hikariConfig.setUsername("user");
hikariConfig.setDriverClassName("org.postgresql.Driver");
hikariConfig.setPassword("password");
return new HikariDataSource(hikariConfig);
}
When i want to use testcontainers for my tests i have to declare a DataSource bean as well:
@Bean
@Primary
DataSource testDataSource() {
if (POSTGRESQL_CONTAINER == null) {
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(DockerImageName.parse("postgres").withTag("9.6.12"));
container.withInitScript("schema.sql");
container.start();
POSTGRESQL_CONTAINER = container;
}
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());
dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());
return dataSource;
}
Using my tests with SpringBootTest i have to ComponentScan nearly all packages because i don´t want to mock all other beans.
So my question is:
Can i somehow exclude the main DataSource only for test cases?
(My current workaround is to define the test DataSource as Primary)
But nevertheless i have two DataSource beans in my Context even if i only need one.