Hantsy
Hantsy

Reputation: 9321

DataSourceBuilder and Spring Testcontainers

I created an example project to try vibur DBCP support in the Spring 3.5.0-M1, it adds Web, JDBC API, TestContainers, Postgres as dependencies, and use Java 21 and Maven to build the project.

The example project can be found here, https://github.com/hantsy/spring6-sandbox/tree/master/boot-vibur-dbcp

Currently, vibur support only exists in the DataSourceBuilder, there is no autoconfiguration like other db pools.

There is a generated Postgres test containers config file which configured a Postges docker instance for dev/test stages.

The DemoApplication class is a startup entry for the application with the generated Postgres instance and empty configuration of connection details(url/username/password), it picked up the default Hiariku pool, and worked well.

@TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfiguration {

    @Bean
    @ServiceConnection
    PostgreSQLContainer<?> postgresContainer() {
        return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"))
                .withExposedPorts(5432);
    }

}

And ViburDataSourceTest use traditional Testcontainers annotations and setup the connection details by Spring DynamicPropertyRegistry, also worked well.

My problem is switching to the newly configured vibur-based DataSource configured with DataSourceBuilder, there is an example for testing Repository - ProductRepository, without the connection URL, etc, it should start the postgres and configure the connection details, but it does not work at all.

# application-test.properties
#spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
#spring.datasource.username=user
#spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
logging.level.org.vibur=DEBUG

The Postgres test containers service did not configure the connection details as expected, the following items failed with null values.

 @Test
 void testDataSource() {
        log.debug("Test dataSourceProperties: url={}, username={}, password={}, driverClassName={}",
                dataSourceProperties.getUrl(),
                dataSourceProperties.getUsername(),
                dataSourceProperties.getPassword(),
                dataSourceProperties.getDriverClassName());
        assertThat(dataSourceProperties.getUrl()).isNotNull();
        assertThat(dataSourceProperties.getUsername()).isNotNull();
        assertThat(dataSourceProperties.getPassword()).isNotNull();
 }

Compared to the DemoApplication which used the same Postgres test containers config but used the default HiariKu pool, it started successfully.

Upvotes: 1

Views: 74

Answers (0)

Related Questions