Currently, in order to use @ServiceConnection in beans along with slice test annotations @ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class) should be added to make it work. This commit register ServiceConnectionAutoConfiguration in specific slice test annotations.

See gh-35252

Comment From: wilkinsona

Thanks for the PR, @eddumelendez.

These changes introduce a cycle between spring-boot-testcontainers and spring-boot-test-autoconfigure:

  • the former has .imports files in src/main/resources for each of the sliced test annotations defined by the latter
  • the latter has tests that make use of @ServiceConnection which is defined by the former

I'm not sure how best to break this cycle. A couple of options:

  1. move all of the tests into spring-boot-testcontainers and remove the test dependency on spring-boot-testcontainers from spring-boot-test-autoconfigure
  2. merge the .imports files into those already in spring-boot-test-autoconfigure but this would require support for an import being "optional" as spring-boot-testcontainers wouldn't necessarily be on the classpath when using spring-boot-test-autoconfigure

1 feels the slightly better of these two options. There may also be others that I've overlooked. I'll discuss things with the team.

Comment From: wilkinsona

Thanks very much for the PR, @eddumelendez. In the end, we went with the second of the two options above. An entry in a .imports file can now be prefixed with optional:. An optional import that is not present on the classpath will be ignored.