Kotlin Spring Boot TestConfiguration vs Primary Datasource Bean Interaction
0 reputation · 16 Jan 2025, 06:04 UTC
Goal: Run Kotlin Spring Boot integration tests that use an in‑memory database without accessing production credentials.
Constraint: The application defines a production DataSource bean annotated with @Primary in src/main/java, while the test suite provides a @TestConfiguration DataSource bean in src/test/java that points to an H2 database.
Uncertainty: When the test class is activated with @ActiveProfiles("test"), it is unclear whether the test‑scoped bean overrides the production @Primary bean, or whether bean definition order and profile precedence allow the production bean to be injected instead.
Specific questions: Does Spring Boot give higher priority to @TestConfiguration beans defined in src/test/java over @Primary beans in src/main/java when the test profile is active? If not, what qualifier or additional @Primary annotation is required to guarantee the test bean is used? How does the interaction of @ActiveProfiles with bean definition order affect this resolution?