diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySource.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySource.java index 1994303645e..10bcb2a9864 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySource.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySource.java @@ -115,7 +115,7 @@ public class TestcontainersPropertySource extends EnumerablePropertySource new EventPublisherRegistrar(environment))); } @@ -138,7 +138,7 @@ public class TestcontainersPropertySource extends EnumerablePropertySource propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME); assertThat(propertySource).isNotNull(); } @Test - void attachWhenAlreadyAttachedReturnsExisting() { + void attachToEnvironmentWhenAlreadyAttachedReturnsExisting() { DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment); PropertySource p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME); DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment); @@ -106,6 +115,24 @@ class TestcontainersPropertySourceTests { assertThat(p1).isSameAs(p2); } + @Test + void attachToEnvironmentAndContextWhenNotAttachedAttaches() { + TestcontainersPropertySource.attach(this.environment, this.context); + PropertySource propertySource = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME); + assertThat(propertySource).isNotNull(); + assertThat(this.context.containsBean(EventPublisherRegistrar.NAME)); + } + + @Test + void attachToEnvironmentAndContextWhenAlreadyAttachedReturnsExisting() { + DynamicPropertyRegistry r1 = TestcontainersPropertySource.attach(this.environment, this.context); + PropertySource p1 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME); + DynamicPropertyRegistry r2 = TestcontainersPropertySource.attach(this.environment, this.context); + PropertySource p2 = this.environment.getPropertySources().get(TestcontainersPropertySource.NAME); + assertThat(r1).isSameAs(r2); + assertThat(p1).isSameAs(p2); + } + @Test void getPropertyPublishesEvent() { try (GenericApplicationContext applicationContext = new GenericApplicationContext()) {