diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java index fa8b553085..8e907ca57e 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java @@ -87,13 +87,24 @@ public class AnnotationsPropertySource extends EnumerablePropertySource List mergedAnnotations = new ArrayList(); for (Annotation annotation : AnnotationUtils.getAnnotations(source)) { if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) { - mergedAnnotations.add(AnnotatedElementUtils.getMergedAnnotation(root, - annotation.annotationType())); + mergedAnnotations + .add(findMergedAnnotation(root, annotation.annotationType())); } } return mergedAnnotations; } + private Annotation findMergedAnnotation(Class source, + Class annotationType) { + if (source == null) { + return null; + } + Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(source, + annotationType); + return mergedAnnotation != null ? mergedAnnotation + : findMergedAnnotation(source.getSuperclass(), annotationType); + } + private void collectProperties(Annotation annotation, Method attribute, PropertyMapping typeMapping, Map properties) { PropertyMapping attributeMapping = AnnotationUtils.getAnnotation(attribute, diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java index 6335b50191..62b4ba334f 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySourceTests.java @@ -169,6 +169,22 @@ public class AnnotationsPropertySourceTests { new AnnotationsPropertySource(PropertyMappedWithSelfAnnotatingAnnotation.class); } + @Test + public void typeLevelAnnotationOnSuperClass() { + AnnotationsPropertySource source = new AnnotationsPropertySource( + PropertyMappedAnnotationOnSuperClass.class); + assertThat(source.getPropertyNames()).containsExactly("value"); + assertThat(source.getProperty("value")).isEqualTo("abc"); + } + + @Test + public void aliasedPropertyMappedAttributeOnSuperClass() { + AnnotationsPropertySource source = new AnnotationsPropertySource( + AliasedPropertyMappedAnnotationOnSuperClass.class); + assertThat(source.getPropertyNames()).containsExactly("aliasing.value"); + assertThat(source.getProperty("aliasing.value")).isEqualTo("baz"); + } + static class NoAnnotation { } @@ -359,4 +375,13 @@ public class AnnotationsPropertySourceTests { } + static class PropertyMappedAnnotationOnSuperClass extends TypeLevel { + + } + + static class AliasedPropertyMappedAnnotationOnSuperClass + extends PropertyMappedAttributeWithAnAlias { + + } + }