From 23876d8a325fc15e254b85dfb61007ae0f7d078f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Ledoyen?= Date: Mon, 8 Nov 2021 19:00:54 +0100 Subject: [PATCH 1/2] Ignore JUnit annotations when caching test contexts Update `ImportsContextCustomizer` to ignore JUnit annotations. See gh-28563 --- .../boot/test/context/ImportsContextCustomizer.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java index 3d33b764ce5e..e1e013d8c60b 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java @@ -222,6 +222,7 @@ static class ContextCustomizerKey { filters.add(new JavaLangAnnotationFilter()); filters.add(new KotlinAnnotationFilter()); filters.add(new SpockAnnotationFilter()); + filters.add(new JunitAnnotationFilter()); ANNOTATION_FILTERS = Collections.unmodifiableSet(filters); } @@ -384,4 +385,15 @@ public boolean isIgnored(Annotation annotation) { } + /** + * {@link AnnotationFilter} for Spock annotations. + */ + private static final class JunitAnnotationFilter implements AnnotationFilter { + + @Override + public boolean isIgnored(Annotation annotation) { + return annotation.annotationType().getName().startsWith("org.junit."); + } + + } } From 8a14e1e2e9989cfa91f39e3c72b84d57e1f87a63 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 8 Nov 2021 13:00:31 -0800 Subject: [PATCH 2/2] Polish 'Ignore JUnit annotations when caching test contexts' See gh-28563 --- .../context/ImportsContextCustomizer.java | 5 +++-- .../ImportsContextCustomizerTests.java | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java index e1e013d8c60b..97e1b3b4ffc3 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -386,7 +386,7 @@ public boolean isIgnored(Annotation annotation) { } /** - * {@link AnnotationFilter} for Spock annotations. + * {@link AnnotationFilter} for JUnit annotations. */ private static final class JunitAnnotationFilter implements AnnotationFilter { @@ -396,4 +396,5 @@ public boolean isIgnored(Annotation annotation) { } } + } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java index 01f048b7d602..6ed9f8419f26 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ import java.util.Set; import kotlin.Metadata; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.spockframework.runtime.model.SpecMetadata; import spock.lang.Issue; @@ -72,6 +74,12 @@ void customizersForTestClassesWithDifferentSpockLangAnnotationsAreEqual() { .isEqualTo(new ImportsContextCustomizer(SecondSpockLangAnnotatedTestClass.class)); } + @Test + void customizersForTestClassesWithDifferentJunitAnnotationsAreEqual() { + assertThat(new ImportsContextCustomizer(FirstJunitAnnotatedTestClass.class)) + .isEqualTo(new ImportsContextCustomizer(SecondJunitAnnotatedTestClass.class)); + } + @Import(TestImportSelector.class) @Indicator1 static class FirstImportSelectorAnnotatedClass { @@ -126,6 +134,18 @@ static class SecondSpockLangAnnotatedTestClass { } + @Nested + @Import(TestImportSelector.class) + static class FirstJunitAnnotatedTestClass { + + } + + @Tag("test") + @Import(TestImportSelector.class) + static class SecondJunitAnnotatedTestClass { + + } + @Retention(RetentionPolicy.RUNTIME) @interface Indicator1 {