Skip to content

Commit e01e4f1

Browse files
committed
Search implemented interfaces on superclass for @Serviceconnection
Refine original fix to also search interfaces on the superclass. Fixes gh-37671
1 parent 084bd3a commit e01e4f1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
5454
}
5555

5656
private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sources) {
57-
ReflectionUtils.doWithFields(clazz, (field) -> {
57+
if (clazz == Object.class || clazz == null) {
58+
return;
59+
}
60+
ReflectionUtils.doWithLocalFields(clazz, (field) -> {
5861
MergedAnnotations annotations = MergedAnnotations.from(field);
5962
annotations.stream(ServiceConnection.class)
6063
.forEach((annotation) -> sources.add(createSource(field, annotation)));
@@ -65,6 +68,7 @@ private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sour
6568
for (Class<?> implementedInterface : clazz.getInterfaces()) {
6669
findSources(implementedInterface, sources);
6770
}
71+
findSources(clazz.getSuperclass(), sources);
6872
}
6973

7074
@SuppressWarnings("unchecked")

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturns
9494
assertThat(customizer.getSources()).hasSize(2);
9595
}
9696

97+
@Test
98+
void createContextCustomizerWhenInheritedImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
99+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
100+
.createContextCustomizer(ServiceConnectionsImplSubclass.class, null);
101+
assertThat(customizer).isNotNull();
102+
assertThat(customizer.getSources()).hasSize(2);
103+
}
104+
97105
@Test
98106
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() {
99107
assertThatIllegalStateException()
@@ -186,6 +194,10 @@ static class ServiceConnectionsImpl implements ServiceConnectionsInterface {
186194

187195
}
188196

197+
static class ServiceConnectionsImplSubclass extends ServiceConnectionsImpl {
198+
199+
}
200+
189201
static class NonStaticServiceConnection {
190202

191203
@ServiceConnection

0 commit comments

Comments
 (0)