2323import org .junit .jupiter .api .Test ;
2424
2525import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
26+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
27+ import org .springframework .boot .testcontainers .properties .TestcontainersPropertySource .EventPublisherRegistrar ;
2628import org .springframework .context .ApplicationEvent ;
2729import org .springframework .context .support .GenericApplicationContext ;
2830import org .springframework .core .env .EnumerablePropertySource ;
@@ -42,6 +44,13 @@ class TestcontainersPropertySourceTests {
4244
4345 private MockEnvironment environment = new MockEnvironment ();
4446
47+ private GenericApplicationContext context = new GenericApplicationContext ();
48+
49+ TestcontainersPropertySourceTests () {
50+ ((DefaultListableBeanFactory ) this .context .getBeanFactory ()).setAllowBeanDefinitionOverriding (false );
51+ this .context .setEnvironment (this .environment );
52+ }
53+
4554 @ Test
4655 void getPropertyWhenHasValueSupplierReturnsSuppliedValue () {
4756 DynamicPropertyRegistry registry = TestcontainersPropertySource .attach (this .environment );
@@ -90,14 +99,14 @@ void getSourceReturnsImmutableSource() {
9099 }
91100
92101 @ Test
93- void attachWhenNotAttachedAttaches () {
102+ void attachToEnvironmentWhenNotAttachedAttaches () {
94103 TestcontainersPropertySource .attach (this .environment );
95104 PropertySource <?> propertySource = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
96105 assertThat (propertySource ).isNotNull ();
97106 }
98107
99108 @ Test
100- void attachWhenAlreadyAttachedReturnsExisting () {
109+ void attachToEnvironmentWhenAlreadyAttachedReturnsExisting () {
101110 DynamicPropertyRegistry r1 = TestcontainersPropertySource .attach (this .environment );
102111 PropertySource <?> p1 = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
103112 DynamicPropertyRegistry r2 = TestcontainersPropertySource .attach (this .environment );
@@ -106,6 +115,24 @@ void attachWhenAlreadyAttachedReturnsExisting() {
106115 assertThat (p1 ).isSameAs (p2 );
107116 }
108117
118+ @ Test
119+ void attachToEnvironmentAndContextWhenNotAttachedAttaches () {
120+ TestcontainersPropertySource .attach (this .environment , this .context );
121+ PropertySource <?> propertySource = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
122+ assertThat (propertySource ).isNotNull ();
123+ assertThat (this .context .containsBean (EventPublisherRegistrar .NAME ));
124+ }
125+
126+ @ Test
127+ void attachToEnvironmentAndContextWhenAlreadyAttachedReturnsExisting () {
128+ DynamicPropertyRegistry r1 = TestcontainersPropertySource .attach (this .environment , this .context );
129+ PropertySource <?> p1 = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
130+ DynamicPropertyRegistry r2 = TestcontainersPropertySource .attach (this .environment , this .context );
131+ PropertySource <?> p2 = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
132+ assertThat (r1 ).isSameAs (r2 );
133+ assertThat (p1 ).isSameAs (p2 );
134+ }
135+
109136 @ Test
110137 void getPropertyPublishesEvent () {
111138 try (GenericApplicationContext applicationContext = new GenericApplicationContext ()) {
0 commit comments