Using the new @ServiceConnection does not work as I would expect when applied within an interface. It starts the container on a random port rather than my spring defined port.
@DynamicPropertySource does work as I expect.
public interface PostgresContainer {
@Container
// @ServiceConnection
PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:11.1");
@DynamicPropertySource
static void initialize(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
}
}
Comment From: eddumelendez
It starts the container on a random port rather than my spring defined port.
Can you elaborate more? I think I didn't get it.
Take into account Testcontainers starts containers in random ports and @ServiceConnection maps the properties from the container to spring boot
Comment From: philwebb
Thanks @eddumelendez, I was just writing the same thing!
@therealaleko perhaps you could provide a sample application that shows the problem?
Comment From: therealaleko
Please check the repo here. Thanks! https://github.com/therealaleko/spring_boot_issue_37671
@philwebb @eddumelendez
Comment From: eddumelendez
Thanks! So, the ServiceConnection doesn't work when using interfaces
I think this line here
https://github.com/spring-projects/spring-boot/blob/d310fb6fce1bceed55596cea70186e71eca77655/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java#L57
should change to MergedAnnotations annotations = MergedAnnotations.from(field, SearchStrategy.TYPE_HIERARCHY);
Comment From: andreasevers
Would it also be possible to add support for inherited interfaces? At the moment the test class has to implement the interface directly, but ideally extending a superclass that implements the interface should work too. I could submit a PR if this is something that could be accepted.
Comment From: philwebb
Good spot @andreasevers, I've refined the search algorithm in e01e4f19129ba671ddcbb388efbb2eb2f7ae7d35
Comment From: andreasevers
Wow, thanks Phil! This is great, using it right away!
Comment From: andreasevers
Could this latest commit also get backported into 3.1.x perhaps?
Comment From: wilkinsona
Good catch, @andreasevers. Thank you. I've done that in https://github.com/spring-projects/spring-boot/commit/fcb75b6a1e61dd1772eda2b8a5e40671314c8ff5.
Comment From: andreasevers
Much appreciated 🙏
Comment From: therealaleko
Thank you!