-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
Context
I have an application (recently upgraded to Spring Boot 3.4.0) with two configured data sources, following a setup similar to the one described in this Baeldung article.
Currently, I am using Testcontainers for integration tests, based on the approach outlined in Spring Boot's documentation. These tests mock the second data source, and everything is working perfectly.
After a great initial experience using Testcontainers for testing, I decided to try it for local development. However, this is where I encountered some challenges.
Problem
I followed the steps described in Baeldung's article on Testcontainers support for local development and Spring's blog post on improved Testcontainers support in Spring Boot 3.1.
Below are the classes involved:
public interface OracleContainerConfiguration {
@Container
OracleContainer oracleContainer =
new OracleContainer("gvenzl/oracle-free:slim-faststart")
.withReuse(true);
@DynamicPropertySource
private static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("my-custom.datasource.url", oracleContainer::getJdbcUrl);
registry.add("my-custom.datasource.username", oracleContainer::getUsername);
registry.add("my-custom.datasource.password", oracleContainer::getPassword);
}
}
@TestConfiguration(proxyBeanMethods = false)
@ImportTestcontainers(OracleContainerConfiguration.class)
public class LocalDevelopmentContainersConfiguration {}
public class MyApplicationWithContainer {
public static void main(String[] args) {
SpringApplication.from(MyApplication::main)
.withAdditionalProfiles("integration-test-oracle")
.with(LocalDevelopmentContainersConfiguration.class)
.run(args);
}
}
When running the application, I noticed that the data source configuration is executed before the @DynamicPropertySource
, which results in the application picking up the properties defined in my application.yml.
In contrast, during an integration test, the behavior is reversed: the values from the @DynamicPropertySource
are processed first, and only then is the data source configured.
Is this the expected behavior, or could it be a bug when using Testcontainers for local development?
Please let me know if anything is unclear.
Thank you very much!