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
This commit is contained in:
Sam Brannen 2022-09-02 14:40:09 +02:00
parent 1cae054cf5
commit 34635d7751
4 changed files with 17 additions and 18 deletions

View File

@ -198,6 +198,7 @@ public class TestContextAotGenerator {
MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
TestContextBootstrapper testContextBootstrapper =
BootstrapUtils.resolveTestContextBootstrapper(testClass);
registerDeclaredConstructors(testContextBootstrapper.getClass());
return testContextBootstrapper.buildMergedContextConfiguration();
}

View File

@ -44,25 +44,12 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
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 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
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));
}

View File

@ -124,6 +124,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
).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,

View File

@ -21,11 +21,16 @@ import org.junit.runner.RunWith;
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 @@ import static org.assertj.core.api.Assertions.assertThat;
* @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 class BasicSpringVintageTests {
.as("@TestPropertySource").isEqualTo("vintage");
}
public static class CustomXmlBootstrapper extends DefaultTestContextBootstrapper {
@Override
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
return GenericXmlContextLoader.class;
}
}
}