23
23
import org .junit .jupiter .api .Test ;
24
24
25
25
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
26
+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
27
+ import org .springframework .boot .testcontainers .properties .TestcontainersPropertySource .EventPublisherRegistrar ;
26
28
import org .springframework .context .ApplicationEvent ;
27
29
import org .springframework .context .support .GenericApplicationContext ;
28
30
import org .springframework .core .env .EnumerablePropertySource ;
@@ -42,6 +44,13 @@ class TestcontainersPropertySourceTests {
42
44
43
45
private MockEnvironment environment = new MockEnvironment ();
44
46
47
+ private GenericApplicationContext context = new GenericApplicationContext ();
48
+
49
+ TestcontainersPropertySourceTests () {
50
+ ((DefaultListableBeanFactory ) this .context .getBeanFactory ()).setAllowBeanDefinitionOverriding (false );
51
+ this .context .setEnvironment (this .environment );
52
+ }
53
+
45
54
@ Test
46
55
void getPropertyWhenHasValueSupplierReturnsSuppliedValue () {
47
56
DynamicPropertyRegistry registry = TestcontainersPropertySource .attach (this .environment );
@@ -90,14 +99,14 @@ void getSourceReturnsImmutableSource() {
90
99
}
91
100
92
101
@ Test
93
- void attachWhenNotAttachedAttaches () {
102
+ void attachToEnvironmentWhenNotAttachedAttaches () {
94
103
TestcontainersPropertySource .attach (this .environment );
95
104
PropertySource <?> propertySource = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
96
105
assertThat (propertySource ).isNotNull ();
97
106
}
98
107
99
108
@ Test
100
- void attachWhenAlreadyAttachedReturnsExisting () {
109
+ void attachToEnvironmentWhenAlreadyAttachedReturnsExisting () {
101
110
DynamicPropertyRegistry r1 = TestcontainersPropertySource .attach (this .environment );
102
111
PropertySource <?> p1 = this .environment .getPropertySources ().get (TestcontainersPropertySource .NAME );
103
112
DynamicPropertyRegistry r2 = TestcontainersPropertySource .attach (this .environment );
@@ -106,6 +115,24 @@ void attachWhenAlreadyAttachedReturnsExisting() {
106
115
assertThat (p1 ).isSameAs (p2 );
107
116
}
108
117
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
+
109
136
@ Test
110
137
void getPropertyPublishesEvent () {
111
138
try (GenericApplicationContext applicationContext = new GenericApplicationContext ()) {
0 commit comments