I am using spring-boot-3.1.0-SNAPSHOT with Spring Data JPA, Postgres, Testcontainers.
For local development, I have created TestcontainersConfiguration.java and TestApplication.java under src/test/java as follows:
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>("postgres:15.2-alpine");
}
}
public class TestApplication {
public static void main(String[] args) {
SpringApplication
.from(Application::main)
.with(TestcontainersConfiguration.class)
.run(args);
}
}
When I run TestApplication.java from IDE then Testcontainers is starting the Postgres database and application is working fine.
Problem:
But, if I add spring-boot-devtools dependency and run TestApplication.java then Testcontainers integration is stopped working. ie, Postgres container is not being started and application startup is failing.
Reproducer: https://github.com/sivaprasadreddy/spring-boot-testcontainers-devmode
In pom.xml uncomment spring-boot-devtools dependency and run to reproduce the problem.
Comment From: eddumelendez
According to my local tests. It was broken via this commit 2b261e6ebd when using spring-boot-devtools
Comment From: philwebb
Thanks for trying the snapshots @sivaprasadreddy, and thanks @eddumelendez for tracking down the commit. I think I know what's causing this. There's a bug in org.springframework.boot.devtools.restart.MainMethod where it stops at the first main that it finds rather than the last. This means that the app gets restarted with Application.main rather than TestApplication.main. Before fixing #35206, the restarted app would find the configuration because of @ComponentScan.