Skip to content

Commit d496170

Browse files
committed
Adapt to change in Spring Framework
See spring-projects/spring-framework#34513 See gh-44481
1 parent fa621f2 commit d496170

File tree

3 files changed

+29
-74
lines changed

3 files changed

+29
-74
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

+11-47
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@
1919
import java.lang.reflect.Method;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22-
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.List;
2524
import java.util.function.Consumer;
2625

27-
import org.springframework.aot.generate.GenerationContext;
2826
import org.springframework.aot.hint.ExecutableMode;
29-
import org.springframework.aot.hint.ReflectionHints;
27+
import org.springframework.aot.hint.RuntimeHints;
3028
import org.springframework.beans.BeanUtils;
31-
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
32-
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
33-
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
34-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3529
import org.springframework.boot.ApplicationContextFactory;
3630
import org.springframework.boot.Banner;
3731
import org.springframework.boot.ConfigurableBootstrapContext;
@@ -113,28 +107,33 @@ public class SpringBootContextLoader extends AbstractContextLoader implements Ao
113107

114108
@Override
115109
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
116-
return loadContext(mergedConfig, Mode.STANDARD, null);
110+
return loadContext(mergedConfig, Mode.STANDARD, null, null);
117111
}
118112

119113
@Override
120-
public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception {
121-
return loadContext(mergedConfig, Mode.AOT_PROCESSING, null);
114+
public ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig,
115+
RuntimeHints runtimeHints) throws Exception {
116+
return loadContext(mergedConfig, Mode.AOT_PROCESSING, null, runtimeHints);
122117
}
123118

124119
@Override
125120
public ApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig,
126121
ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception {
127-
return loadContext(mergedConfig, Mode.AOT_RUNTIME, initializer);
122+
return loadContext(mergedConfig, Mode.AOT_RUNTIME, initializer, null);
128123
}
129124

130125
private ApplicationContext loadContext(MergedContextConfiguration mergedConfig, Mode mode,
131-
ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception {
126+
ApplicationContextInitializer<ConfigurableApplicationContext> initializer, RuntimeHints runtimeHints)
127+
throws Exception {
132128
assertHasClassesOrLocations(mergedConfig);
133129
SpringBootTestAnnotation annotation = SpringBootTestAnnotation.get(mergedConfig);
134130
String[] args = annotation.getArgs();
135131
UseMainMethod useMainMethod = annotation.getUseMainMethod();
136132
Method mainMethod = getMainMethod(mergedConfig, useMainMethod);
137133
if (mainMethod != null) {
134+
if (runtimeHints != null) {
135+
runtimeHints.reflection().registerMethod(mainMethod, ExecutableMode.INVOKE);
136+
}
138137
ContextLoaderHook hook = new ContextLoaderHook(mode, initializer,
139138
(application) -> configure(mergedConfig, application));
140139
return hook.runMain(() -> ReflectionUtils.invokeMethod(mainMethod, null, new Object[] { args }));
@@ -585,39 +584,4 @@ private ApplicationContext run(ThrowingSupplier<ConfigurableApplicationContext>
585584

586585
}
587586

588-
static class MainMethodBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
589-
590-
@Override
591-
public BeanFactoryInitializationAotContribution processAheadOfTime(
592-
ConfigurableListableBeanFactory beanFactory) {
593-
List<Method> mainMethods = new ArrayList<>();
594-
for (String beanName : beanFactory.getBeanDefinitionNames()) {
595-
Class<?> beanType = beanFactory.getType(beanName);
596-
Method mainMethod = findMainMethod(beanType);
597-
if (mainMethod != null) {
598-
mainMethods.add(mainMethod);
599-
}
600-
}
601-
return !mainMethods.isEmpty() ? new AotContribution(mainMethods) : null;
602-
}
603-
604-
static class AotContribution implements BeanFactoryInitializationAotContribution {
605-
606-
private final Collection<Method> mainMethods;
607-
608-
AotContribution(Collection<Method> mainMethods) {
609-
this.mainMethods = mainMethods;
610-
}
611-
612-
@Override
613-
public void applyTo(GenerationContext generationContext,
614-
BeanFactoryInitializationCode beanFactoryInitializationCode) {
615-
ReflectionHints reflectionHints = generationContext.getRuntimeHints().reflection();
616-
this.mainMethods.forEach((method) -> reflectionHints.registerMethod(method, ExecutableMode.INVOKE));
617-
}
618-
619-
}
620-
621-
}
622-
623587
}

spring-boot-project/spring-boot-test/src/main/resources/META-INF/spring/aot.factories

-2
This file was deleted.

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java

+18-25
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@
2727

2828
import org.springframework.aot.hint.RuntimeHints;
2929
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
30-
import org.springframework.aot.test.generate.TestGenerationContext;
3130
import org.springframework.beans.factory.BeanCreationException;
32-
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
33-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3431
import org.springframework.boot.ApplicationContextFactory;
3532
import org.springframework.boot.SpringApplication;
3633
import org.springframework.boot.SpringBootConfiguration;
37-
import org.springframework.boot.test.context.SpringBootContextLoader.MainMethodBeanFactoryInitializationAotProcessor;
3834
import org.springframework.boot.test.context.SpringBootTest.UseMainMethod;
3935
import org.springframework.boot.test.util.TestPropertyValues;
4036
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
@@ -47,6 +43,7 @@
4743
import org.springframework.core.env.StandardEnvironment;
4844
import org.springframework.test.context.ActiveProfiles;
4945
import org.springframework.test.context.ApplicationContextFailureProcessor;
46+
import org.springframework.test.context.BootstrapUtils;
5047
import org.springframework.test.context.ContextConfiguration;
5148
import org.springframework.test.context.ContextHierarchy;
5249
import org.springframework.test.context.MergedContextConfiguration;
@@ -59,6 +56,8 @@
5956

6057
import static org.assertj.core.api.Assertions.assertThat;
6158
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
59+
import static org.mockito.BDDMockito.then;
60+
import static org.mockito.Mockito.mock;
6261

6362
/**
6463
* Tests for {@link SpringBootContextLoader}
@@ -255,30 +254,24 @@ void whenUseMainMethodWithContextHierarchyThrowsException() {
255254
}
256255

257256
@Test
258-
void whenMainMethodPresentRegisterReflectionHints() {
259-
TestContext testContext = new ExposedTestContextManager(UseMainMethodWhenAvailableAndNoMainMethod.class)
260-
.getExposedTestContext();
261-
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) testContext
262-
.getApplicationContext()
263-
.getAutowireCapableBeanFactory();
264-
BeanFactoryInitializationAotContribution aotContribution = new MainMethodBeanFactoryInitializationAotProcessor()
265-
.processAheadOfTime(beanFactory);
266-
assertThat(aotContribution).isNull();
257+
void whenMainMethodNotAvailableReturnsNoAotContribution() throws Exception {
258+
SpringBootContextLoader contextLoader = new SpringBootContextLoader();
259+
MergedContextConfiguration contextConfiguration = BootstrapUtils
260+
.resolveTestContextBootstrapper(UseMainMethodWhenAvailableAndNoMainMethod.class)
261+
.buildMergedContextConfiguration();
262+
RuntimeHints runtimeHints = mock(RuntimeHints.class);
263+
contextLoader.loadContextForAotProcessing(contextConfiguration, runtimeHints);
264+
then(runtimeHints).shouldHaveNoInteractions();
267265
}
268266

269267
@Test
270-
void whenMainMethodNotAvailableReturnsNoAotContribution() {
271-
TestContext testContext = new ExposedTestContextManager(UseMainMethodWhenAvailableAndMainMethod.class)
272-
.getExposedTestContext();
273-
ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) testContext
274-
.getApplicationContext()
275-
.getAutowireCapableBeanFactory();
276-
BeanFactoryInitializationAotContribution aotContribution = new MainMethodBeanFactoryInitializationAotProcessor()
277-
.processAheadOfTime(beanFactory);
278-
assertThat(aotContribution).isNotNull();
279-
TestGenerationContext generationContext = new TestGenerationContext();
280-
aotContribution.applyTo(generationContext, null);
281-
RuntimeHints runtimeHints = generationContext.getRuntimeHints();
268+
void whenMainMethodPresentRegisterReflectionHints() throws Exception {
269+
SpringBootContextLoader contextLoader = new SpringBootContextLoader();
270+
MergedContextConfiguration contextConfiguration = BootstrapUtils
271+
.resolveTestContextBootstrapper(UseMainMethodWhenAvailableAndMainMethod.class)
272+
.buildMergedContextConfiguration();
273+
RuntimeHints runtimeHints = new RuntimeHints();
274+
contextLoader.loadContextForAotProcessing(contextConfiguration, runtimeHints);
282275
assertThat(RuntimeHintsPredicates.reflection().onMethod(ConfigWithMain.class, "main").invoke())
283276
.accepts(runtimeHints);
284277
}

0 commit comments

Comments
 (0)