3

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.

Thomas Lang
  • 1,285
  • 17
  • 35
  • 1
    Does this answer your question? [exclude @Component from @ComponentScan](https://stackoverflow.com/questions/18992880/exclude-component-from-componentscan) – Jens Schauder May 07 '21 at 06:38
  • Thank you. I will try this out. By the way. Have you checked out Wim Deblauwe's answer below? There is no dedicated `DataSource` declared in this code. Isn´t this oblitgatory in Spring Data JDBC? I´m just curious ... – Thomas Lang May 10 '21 at 05:23
  • 1
    Spring Boot can create your datasource, which seems to get utilized with Wims answer, where the necessary information is configured in the dynamic property source. – Jens Schauder May 10 '21 at 06:20

1 Answers1

7

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);
    }
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • Very nice. I can´t find a dedicated spring-data-jdbc `DataSource` bean in your code. Does this line the job? https://github.com/wearearima/spring-data-jdbc-testcontainers/blob/master/src/main/java/eu/arima/springdatajdbctestcontainers/SpringDataJdbcTestcontainersApplication.java#L15 – Thomas Lang May 06 '21 at 07:17
  • 1
    It is not my code, just something I found googling, so I can't help you on that unfortunately. – Wim Deblauwe May 06 '21 at 07:29