Search code examples
spring-boottestcontainersspring-data-jdbc

Spring Data JDBC Testcontainers DataSource


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.


Solution

  • The example at https://github.com/wearearima/spring-data-jdbc-testcontainers seems to indicate that it is not needed to define your own DataSource at all.

    See https://github.com/wearearima/spring-data-jdbc-testcontainers/blob/master/src/test/java/eu/arima/springdatajdbctestcontainers/AccountRepositoryTest.java#L94 how to pass the properties of the TestContainers managed database to Spring Boot:

     @DynamicPropertySource
        static void postgresqlProperties(DynamicPropertyRegistry registry) {
            registry.add("spring.datasource.url", postgresqlContainer::getJdbcUrl);
            registry.add("spring.datasource.username", postgresqlContainer::getUsername);
            registry.add("spring.datasource.password", postgresqlContainer::getPassword);
        }