Skip to content

Commit 86216fb

Browse files
Search implemented interfaces for @Serviceconnection fields
Fixes gh-37671
1 parent 4271e6d commit 86216fb

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @author Moritz Halbritter
4242
* @author Andy Wilkinson
4343
* @author Phillip Webb
44+
* @author Scott Frederick
4445
*/
4546
class ServiceConnectionContextCustomizerFactory implements ContextCustomizerFactory {
4647

@@ -61,6 +62,9 @@ private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sour
6162
if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
6263
findSources(clazz.getEnclosingClass(), sources);
6364
}
65+
for (Class<?> implementedInterface : clazz.getInterfaces()) {
66+
findSources(implementedInterface, sources);
67+
}
6468
}
6569

6670
@SuppressWarnings("unchecked")

spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactoryTests.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,39 @@ void createContextCustomizerWhenEnclosingClassHasServiceConnectionsReturnsCustom
7171
}
7272

7373
@Test
74-
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHepfulException() {
74+
void createContextCustomizerWhenInterfaceHasServiceConnectionsReturnsCustomizer() {
75+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
76+
.createContextCustomizer(ServiceConnectionsInterface.class, null);
77+
assertThat(customizer).isNotNull();
78+
assertThat(customizer.getSources()).hasSize(2);
79+
}
80+
81+
@Test
82+
void createContextCustomizerWhenSuperclassHasServiceConnectionsReturnsCustomizer() {
83+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
84+
.createContextCustomizer(ServiceConnectionsSubclass.class, null);
85+
assertThat(customizer).isNotNull();
86+
assertThat(customizer.getSources()).hasSize(2);
87+
}
88+
89+
@Test
90+
void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
91+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
92+
.createContextCustomizer(ServiceConnectionsImpl.class, null);
93+
assertThat(customizer).isNotNull();
94+
assertThat(customizer.getSources()).hasSize(2);
95+
}
96+
97+
@Test
98+
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() {
7599
assertThatIllegalStateException()
76100
.isThrownBy(() -> this.factory.createContextCustomizer(NonStaticServiceConnection.class, null))
77101
.withMessage("@ServiceConnection field 'service' must be static");
78102

79103
}
80104

81105
@Test
82-
void createContextCustomizerWhenClassHasAnnotationOnNonConnectionFieldFailsWithHepfulException() {
106+
void createContextCustomizerWhenClassHasAnnotationOnNonConnectionFieldFailsWithHelpfulException() {
83107
assertThatIllegalStateException()
84108
.isThrownBy(() -> this.factory.createContextCustomizer(ServiceConnectionOnWrongFieldType.class, null))
85109
.withMessage("Field 'service2' in " + ServiceConnectionOnWrongFieldType.class.getName()
@@ -141,6 +165,27 @@ class NestedClass {
141165

142166
}
143167

168+
interface ServiceConnectionsInterface {
169+
170+
@ServiceConnection
171+
Container<?> service1 = new MockContainer();
172+
173+
@ServiceConnection
174+
Container<?> service2 = new MockContainer();
175+
176+
default void dummy() {
177+
}
178+
179+
}
180+
181+
static class ServiceConnectionsSubclass extends ServiceConnections {
182+
183+
}
184+
185+
static class ServiceConnectionsImpl implements ServiceConnectionsInterface {
186+
187+
}
188+
144189
static class NonStaticServiceConnection {
145190

146191
@ServiceConnection

0 commit comments

Comments
 (0)