From 34635d77511936707fa23e868914eaf3418bce23 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 2 Sep 2022 14:40:09 +0200 Subject: [PATCH] Register runtime hints for TestContextBootstrappers This commit automatically registers runtime hints for TestContextBootstrapper classes, including default bootstrappers as well as any bootstrapper configured via @BootstrapWith. Closes gh-29023 --- .../context/aot/TestContextAotGenerator.java | 1 + .../aot/hint/TestContextRuntimeHints.java | 19 +------------------ .../aot/TestContextAotGeneratorTests.java | 1 + .../basic/BasicSpringVintageTests.java | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java index 516bdd80bfbf..ca1c856c7837 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java @@ -198,6 +198,7 @@ private GenericApplicationContext loadContextForAotProcessing( MergedContextConfiguration buildMergedContextConfiguration(Class testClass) { TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(testClass); + registerDeclaredConstructors(testContextBootstrapper.getClass()); return testContextBootstrapper.buildMergedContextConfiguration(); } diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/hint/TestContextRuntimeHints.java b/spring-test/src/main/java/org/springframework/test/context/aot/hint/TestContextRuntimeHints.java index 86c03df0cff9..d1ceaa6b54d8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/hint/TestContextRuntimeHints.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/hint/TestContextRuntimeHints.java @@ -44,25 +44,12 @@ public void registerHints(RuntimeHints runtimeHints, ClassLoader classLoader) { ReflectionHints reflectionHints = runtimeHints.reflection(); + // Loaded reflectively in BootstrapUtils registerPublicConstructors(reflectionHints, - // Loaded reflectively in BootstrapUtils org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.class, - // Loaded reflectively in BootstrapUtils org.springframework.test.context.support.DefaultBootstrapContext.class ); - registerDeclaredConstructors(reflectionHints, - // Loaded reflectively in BootstrapUtils - org.springframework.test.context.support.DefaultTestContextBootstrapper.class - ); - - if (servletPresent) { - registerDeclaredConstructors(reflectionHints, - // Loaded reflectively in BootstrapUtils - "org.springframework.test.context.web.WebTestContextBootstrapper" - ); - } - if (groovyPresent) { registerDeclaredConstructors(reflectionHints, // Loaded reflectively in DelegatingSmartContextLoader @@ -90,10 +77,6 @@ private static void registerPublicConstructors(ReflectionHints reflectionHints, reflectionHints.registerTypes(types, TypeHint.builtWith(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); } - private static void registerDeclaredConstructors(ReflectionHints reflectionHints, Class... types) { - registerDeclaredConstructors(reflectionHints, TypeReference.listOf(types)); - } - private static void registerDeclaredConstructors(ReflectionHints reflectionHints, String... classNames) { registerDeclaredConstructors(reflectionHints, listOf(classNames)); } diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java index 1448f4c8fb12..9204976146a7 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java @@ -124,6 +124,7 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) { ).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_PUBLIC_CONSTRUCTORS)); Stream.of( + org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper.class, org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests.CustomInitializer.class, org.springframework.test.context.support.AnnotationConfigContextLoader.class, org.springframework.test.context.support.DefaultTestContextBootstrapper.class, diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.java index 709d20fddc55..2a1ffde57b69 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.java @@ -21,11 +21,16 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; +import org.springframework.test.context.BootstrapWith; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextLoader; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper; import org.springframework.test.context.aot.samples.common.MessageService; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; +import org.springframework.test.context.support.DefaultTestContextBootstrapper; +import org.springframework.test.context.support.GenericXmlContextLoader; import static org.assertj.core.api.Assertions.assertThat; @@ -33,7 +38,9 @@ * @author Sam Brannen * @since 6.0 */ +@BootstrapWith(CustomXmlBootstrapper.class) @RunWith(SpringRunner.class) +// Override the default loader configured by the CustomXmlBootstrapper @ContextConfiguration(classes = BasicTestConfiguration.class, loader = AnnotationConfigContextLoader.class) @TestPropertySource(properties = "test.engine = vintage") public class BasicSpringVintageTests { @@ -55,4 +62,11 @@ public void test() { .as("@TestPropertySource").isEqualTo("vintage"); } + public static class CustomXmlBootstrapper extends DefaultTestContextBootstrapper { + @Override + protected Class getDefaultContextLoaderClass(Class testClass) { + return GenericXmlContextLoader.class; + } + } + }