Skip to content

Commit b8f69eb

Browse files
committed
Register test bean override infrastructure beans as singletons
Prior to this commit, AOT processing failed for tests that made use of test bean override features, since the Set<OverrideMetadata> constructor argument configured in the bean definition for the BeanOverrideBeanFactoryPostProcessor could not be handled by our AOT support. To address that, this commit registers test bean override infrastructure beans as singletons instead of via bean definitions with the infrastructure role. See spring-projectsgh-32933
1 parent 13e175a commit b8f69eb

File tree

2 files changed

+7
-38
lines changed

2 files changed

+7
-38
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/BeanOverrideContextCustomizer.java

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
package org.springframework.test.context.bean.override;
1818

1919
import java.util.Set;
20-
import java.util.function.Consumer;
2120

22-
import org.springframework.beans.factory.config.BeanDefinition;
23-
import org.springframework.beans.factory.config.ConstructorArgumentValues;
24-
import org.springframework.beans.factory.config.RuntimeBeanReference;
25-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
26-
import org.springframework.beans.factory.support.RootBeanDefinition;
21+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2722
import org.springframework.context.ConfigurableApplicationContext;
2823
import org.springframework.test.context.ContextCustomizer;
2924
import org.springframework.test.context.MergedContextConfiguration;
@@ -57,43 +52,18 @@ class BeanOverrideContextCustomizer implements ContextCustomizer {
5752

5853
@Override
5954
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
60-
if (!(context instanceof BeanDefinitionRegistry registry)) {
61-
throw new IllegalStateException("Cannot process bean overrides with an ApplicationContext " +
62-
"that doesn't implement BeanDefinitionRegistry: " + context.getClass());
63-
}
64-
registerInfrastructure(registry);
55+
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
56+
BeanOverrideRegistrar beanOverrideRegistrar = new BeanOverrideRegistrar();
57+
beanOverrideRegistrar.setBeanFactory(beanFactory);
58+
beanFactory.registerSingleton(REGISTRAR_BEAN_NAME, beanOverrideRegistrar);
59+
beanFactory.registerSingleton(EARLY_INFRASTRUCTURE_BEAN_NAME, new WrapEarlyBeanPostProcessor(beanOverrideRegistrar));
60+
beanFactory.registerSingleton(INFRASTRUCTURE_BEAN_NAME, new BeanOverrideBeanFactoryPostProcessor(this.metadata, beanOverrideRegistrar));
6561
}
6662

6763
Set<OverrideMetadata> getMetadata() {
6864
return this.metadata;
6965
}
7066

71-
private void registerInfrastructure(BeanDefinitionRegistry registry) {
72-
addInfrastructureBeanDefinition(registry, BeanOverrideRegistrar.class, REGISTRAR_BEAN_NAME,
73-
constructorArgs -> {});
74-
75-
RuntimeBeanReference registrarReference = new RuntimeBeanReference(REGISTRAR_BEAN_NAME);
76-
addInfrastructureBeanDefinition(registry, WrapEarlyBeanPostProcessor.class, EARLY_INFRASTRUCTURE_BEAN_NAME,
77-
constructorArgs -> constructorArgs.addIndexedArgumentValue(0, registrarReference));
78-
addInfrastructureBeanDefinition(registry, BeanOverrideBeanFactoryPostProcessor.class, INFRASTRUCTURE_BEAN_NAME,
79-
constructorArgs -> {
80-
constructorArgs.addIndexedArgumentValue(0, this.metadata);
81-
constructorArgs.addIndexedArgumentValue(1, registrarReference);
82-
});
83-
}
84-
85-
private void addInfrastructureBeanDefinition(BeanDefinitionRegistry registry,
86-
Class<?> clazz, String beanName, Consumer<ConstructorArgumentValues> constructorArgumentsConsumer) {
87-
88-
if (!registry.containsBeanDefinition(beanName)) {
89-
RootBeanDefinition definition = new RootBeanDefinition(clazz);
90-
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
91-
ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
92-
constructorArgumentsConsumer.accept(constructorArguments);
93-
registry.registerBeanDefinition(beanName, definition);
94-
}
95-
}
96-
9767
@Override
9868
public boolean equals(Object other) {
9969
if (other == this) {

spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ void endToEndTestsForEntireSpringTestModule() {
145145
runEndToEndTests(testClasses, false);
146146
}
147147

148-
@Disabled("Comment out to run @TestBean integration tests in AOT mode")
149148
@Test
150149
void endToEndTestsForTestBeanOverrideTestClasses() {
151150
List<Class<?>> testClasses = List.of(

0 commit comments

Comments
 (0)