diff --git a/spring-boot-test/pom.xml b/spring-boot-test/pom.xml index 782cb8c675..e99adc00c8 100644 --- a/spring-boot-test/pom.xml +++ b/spring-boot-test/pom.xml @@ -112,21 +112,11 @@ true - - org.jetbrains.kotlin - kotlin-runtime - test - org.apache.tomcat.embed tomcat-embed-core test - - org.springframework - spring-webmvc - test - org.codehaus.groovy groovy @@ -138,6 +128,20 @@ true test + + org.jetbrains.kotlin + kotlin-runtime + test + + + org.spockframework + spock-core + + + org.springframework + spring-webmvc + test + diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java index 5e1087ed99..0b0000039b 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java @@ -214,6 +214,16 @@ class ImportsContextCustomizer implements ContextCustomizer { */ static class ContextCustomizerKey { + private static final Set ANNOTATION_FILTERS; + + static { + Set filters = new HashSet(); + filters.add(new JavaLangAnnotationFilter()); + filters.add(new KotlinAnnotationFilter()); + filters.add(new SpockAnnotationFilter()); + ANNOTATION_FILTERS = Collections.unmodifiableSet(filters); + } + private final Set annotations; ContextCustomizerKey(Class testClass) { @@ -239,8 +249,7 @@ class ImportsContextCustomizer implements ContextCustomizer { private void collectElementAnnotations(AnnotatedElement element, Set annotations, Set> seen) { for (Annotation annotation : element.getDeclaredAnnotations()) { - if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation) - && !isIgnoredKotlinAnnotation(annotation)) { + if (!isIgnoredAnnotation(annotation)) { annotations.add(annotation); collectClassAnnotations(annotation.annotationType(), annotations, seen); @@ -248,13 +257,13 @@ class ImportsContextCustomizer implements ContextCustomizer { } } - private boolean isIgnoredKotlinAnnotation(Annotation annotation) { - return "kotlin.Metadata".equals(annotation.annotationType().getName()) - || isInKotlinAnnotationPackage(annotation); - } - - private boolean isInKotlinAnnotationPackage(Annotation annotation) { - return annotation.annotationType().getName().startsWith("kotlin.annotation."); + private boolean isIgnoredAnnotation(Annotation annotation) { + for (AnnotationFilter annotationFilter : ANNOTATION_FILTERS) { + if (annotationFilter.isIgnored(annotation)) { + return true; + } + } + return false; } @Override @@ -268,6 +277,46 @@ class ImportsContextCustomizer implements ContextCustomizer { && this.annotations.equals(((ContextCustomizerKey) obj).annotations)); } + private interface AnnotationFilter { + + boolean isIgnored(Annotation annotation); + + } + + private static final class JavaLangAnnotationFilter implements AnnotationFilter { + + @Override + public boolean isIgnored(Annotation annotation) { + return AnnotationUtils.isInJavaLangAnnotationPackage(annotation); + } + + } + + private static final class KotlinAnnotationFilter implements AnnotationFilter { + + @Override + public boolean isIgnored(Annotation annotation) { + return "kotlin.Metadata".equals(annotation.annotationType().getName()) + || isInKotlinAnnotationPackage(annotation); + } + + private boolean isInKotlinAnnotationPackage(Annotation annotation) { + return annotation.annotationType().getName() + .startsWith("kotlin.annotation."); + } + + } + + private static final class SpockAnnotationFilter implements AnnotationFilter { + + @Override + public boolean isIgnored(Annotation annotation) { + return annotation.annotationType().getName() + .startsWith("org.spockframework."); + } + + } + } } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java index dea8eaef1a..aeec7702f6 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.test.context; import kotlin.Metadata; import org.junit.Test; +import org.spockframework.runtime.model.SpecMetadata; import static org.assertj.core.api.Assertions.assertThat; @@ -35,6 +36,13 @@ public class ImportsContextCustomizerTests { SecondKotlinAnnotatedTestClass.class)); } + @Test + public void customizersForTestClassesWithDifferentSpockMetadataAreEqual() { + assertThat(new ImportsContextCustomizer(FirstSpockAnnotatedTestClass.class)) + .isEqualTo(new ImportsContextCustomizer( + SecondSpockAnnotatedTestClass.class)); + } + @Metadata(d2 = "foo") static class FirstKotlinAnnotatedTestClass { @@ -45,4 +53,14 @@ public class ImportsContextCustomizerTests { } + @SpecMetadata(filename = "foo", line = 10) + static class FirstSpockAnnotatedTestClass { + + } + + @SpecMetadata(filename = "bar", line = 10) + static class SecondSpockAnnotatedTestClass { + + } + }