Add support for creating a container from a TestImage value

This commit improves TestImage so that an explicit image can trigger
the creation of the container, with optional additional setup. This
removes the need of creating additional container types for images that
have multiple flavors, and to please the static method that can only
create a container based on a single match.

Closes gh-51082
This commit is contained in:
Stéphane Nicoll
2026-09-08 16:46:48 +02:00
parent f956f588d5
commit b1e91c942e
125 changed files with 201 additions and 356 deletions
@@ -127,7 +127,7 @@ class ImportTestcontainersTests {
static class ImportWithoutValue {
@ContainerAnnotation
static PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
}
@@ -151,14 +151,14 @@ class ImportTestcontainersTests {
@ImportTestcontainers
static class NonStaticContainer {
PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
}
interface ContainerDefinitions {
@ContainerAnnotation
PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
}
@@ -170,7 +170,7 @@ class ImportTestcontainersTests {
@ImportTestcontainers
static class ContainerDefinitionsWithDynamicPropertySource {
static PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
@@ -90,7 +90,7 @@ class TestcontainersImportWithPropertiesInjectedIntoLoadTimeWeaverAwareBeanInteg
static class Containers {
@Container
static PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
@DynamicPropertySource
static void setConnectionProperties(DynamicPropertyRegistry registry) {
@@ -65,7 +65,7 @@ class TestcontainersLifecycleOrderIntegrationTests {
@Bean
@ServiceConnection
RedisContainer redisContainer() {
return TestImage.container(EventRecordingRedisContainer.class);
return TestImage.forContainer(EventRecordingRedisContainer.class);
}
}
@@ -77,7 +77,7 @@ class TestcontainersLifecycleOrderWithScopeIntegrationTests {
@Scope("custom")
@ServiceConnection
RedisContainer redisContainer() {
return TestImage.container(EventRecordingRedisContainer.class);
return TestImage.forContainer(EventRecordingRedisContainer.class);
}
}
@@ -55,17 +55,17 @@ class TestcontainersParallelStartupIntegrationTests {
@Bean
static PostgreSQLContainer container1() {
return TestImage.container(PostgreSQLContainer.class);
return TestImage.forContainer(PostgreSQLContainer.class);
}
@Bean
static PostgreSQLContainer container2() {
return TestImage.container(PostgreSQLContainer.class);
return TestImage.forContainer(PostgreSQLContainer.class);
}
@Bean
static PostgreSQLContainer container3() {
return TestImage.container(PostgreSQLContainer.class);
return TestImage.forContainer(PostgreSQLContainer.class);
}
}
@@ -52,13 +52,13 @@ class TestcontainersParallelStartupWithImportTestcontainersIntegrationTests {
static class Containers {
@Container
static PostgreSQLContainer container1 = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container1 = TestImage.forContainer(PostgreSQLContainer.class);
@Container
static PostgreSQLContainer container2 = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container2 = TestImage.forContainer(PostgreSQLContainer.class);
@Container
static PostgreSQLContainer container3 = TestImage.container(PostgreSQLContainer.class);
static PostgreSQLContainer container3 = TestImage.forContainer(PostgreSQLContainer.class);
}
@@ -67,7 +67,7 @@ class TestcontainersPropertySourceAutoConfigurationTests {
@Bean
RedisContainer redisContainer(DynamicPropertyRegistry properties) {
RedisContainer container = TestImage.container(RedisContainer.class);
RedisContainer container = TestImage.forContainer(RedisContainer.class);
properties.add("container.port", container::getFirstMappedPort);
return container;
}
@@ -81,7 +81,7 @@ class TestcontainersPropertySourceAutoConfigurationTests {
@Bean
RedisContainer redisContainer() {
return TestImage.container(RedisContainer.class);
return TestImage.forContainer(RedisContainer.class);
}
@Bean
@@ -143,7 +143,7 @@ class ServiceConnectionAutoConfigurationTests {
@Bean
@ServiceConnection
PostgreSQLContainer postgresContainer() {
return TestImage.container(PostgreSQLContainer.class);
return TestImage.forContainer(PostgreSQLContainer.class);
}
}
@@ -176,7 +176,7 @@ class ServiceConnectionAutoConfigurationTests {
static class TestcontainersRootBeanDefinition extends RootBeanDefinition implements TestcontainerBeanDefinition {
private final PostgreSQLContainer container = TestImage.container(PostgreSQLContainer.class);
private final PostgreSQLContainer container = TestImage.forContainer(PostgreSQLContainer.class);
TestcontainersRootBeanDefinition() {
setBeanClass(PostgreSQLContainer.class);
@@ -46,7 +46,7 @@ class ServiceConnectionStartsConnectionOnceIntegrationTests {
@Container
@ServiceConnection
static final StartCountingPostgreSQLContainer postgres = TestImage
static final StartCountingPostgreSQLContainer postgres = TestImage.POSTGRESQL
.container(StartCountingPostgreSQLContainer.class);
@Test