Skip to content

Commit

Permalink
Register runtime hints for TestContextBootstrappers
Browse files Browse the repository at this point in the history
This commit automatically registers runtime hints for
TestContextBootstrapper classes, including default bootstrappers as
well as any bootstrapper configured via @BootstrapWith.

Closes gh-29023
  • Loading branch information
sbrannen committed Sep 2, 2022
1 parent 1cae054 commit 34635d7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private GenericApplicationContext loadContextForAotProcessing(
MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
TestContextBootstrapper testContextBootstrapper =
BootstrapUtils.resolveTestContextBootstrapper(testClass);
registerDeclaredConstructors(testContextBootstrapper.getClass());
return testContextBootstrapper.buildMergedContextConfiguration();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@
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;

/**
* @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 {
Expand All @@ -55,4 +62,11 @@ public void test() {
.as("@TestPropertySource").isEqualTo("vintage");
}

public static class CustomXmlBootstrapper extends DefaultTestContextBootstrapper {
@Override
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
return GenericXmlContextLoader.class;
}
}

}

0 comments on commit 34635d7

Please sign in to comment.