From fcb75b6a1e61dd1772eda2b8a5e40671314c8ff5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 14 Oct 2023 19:43:41 -0700 Subject: [PATCH] Search implemented interfaces on superclass for @ServiceConnection Refine original fix to also search interfaces on the superclass. Fixes gh-37671 --- .../ServiceConnectionContextCustomizerFactory.java | 6 +++++- ...rviceConnectionContextCustomizerFactoryTests.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java index 9c991328b4dc..c54c77b04517 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactory.java @@ -54,7 +54,10 @@ public ContextCustomizer createContextCustomizer(Class testClass, } private void findSources(Class clazz, List> sources) { - ReflectionUtils.doWithFields(clazz, (field) -> { + if (clazz == Object.class || clazz == null) { + return; + } + ReflectionUtils.doWithLocalFields(clazz, (field) -> { MergedAnnotations annotations = MergedAnnotations.from(field); annotations.stream(ServiceConnection.class) .forEach((annotation) -> sources.add(createSource(field, annotation))); @@ -65,6 +68,7 @@ private void findSources(Class clazz, List> sour for (Class implementedInterface : clazz.getInterfaces()) { findSources(implementedInterface, sources); } + findSources(clazz.getSuperclass(), sources); } @SuppressWarnings("unchecked") diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactoryTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactoryTests.java index dc487d12c7e5..ba3071e8aa3d 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactoryTests.java +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerFactoryTests.java @@ -94,6 +94,14 @@ void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturns assertThat(customizer.getSources()).hasSize(2); } + @Test + void createContextCustomizerWhenInheritedImplementedInterfaceHasServiceConnectionsReturnsCustomizer() { + ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory + .createContextCustomizer(ServiceConnectionsImplSubclass.class, null); + assertThat(customizer).isNotNull(); + assertThat(customizer.getSources()).hasSize(2); + } + @Test void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() { assertThatIllegalStateException() @@ -186,6 +194,10 @@ static class ServiceConnectionsImpl implements ServiceConnectionsInterface { } + static class ServiceConnectionsImplSubclass extends ServiceConnectionsImpl { + + } + static class NonStaticServiceConnection { @ServiceConnection