Skip to content

Commit d38e4d8

Browse files
committed
Better support for FactoryBeans in BeanOverrideBeanFactoryPostProcessor
This commit makes sure to account for FactoryBean names when registering a bean override. In the case of ReplaceDefinition mode, if there is a factory bean name, it is used to check singleton status and as the name in the registrar. Closes gh-32971
1 parent 416eff1 commit d38e4d8

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.beans.factory.config.DependencyDescriptor;
3535
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
3636
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
37+
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
3738
import org.springframework.beans.factory.support.RootBeanDefinition;
3839
import org.springframework.core.Ordered;
3940
import org.springframework.core.PriorityOrdered;
@@ -128,6 +129,7 @@ private void registerReplaceDefinition(ConfigurableListableBeanFactory beanFacto
128129

129130
RootBeanDefinition beanDefinition = createBeanDefinition(overrideMetadata);
130131
String beanName = overrideMetadata.getBeanName();
132+
String beanNameIncludingFactory;
131133
BeanDefinition existingBeanDefinition = null;
132134
if (beanName == null) {
133135
Set<String> candidateNames = getExistingBeanNamesByType(beanFactory, overrideMetadata, true);
@@ -139,7 +141,8 @@ private void registerReplaceDefinition(ConfigurableListableBeanFactory beanFacto
139141
" (as required by annotated field '" + field.getDeclaringClass().getSimpleName() +
140142
"." + field.getName() + "')" + (candidateCount > 0 ? ": " + candidateNames : ""));
141143
}
142-
beanName = candidateNames.iterator().next();
144+
beanNameIncludingFactory = candidateNames.iterator().next();
145+
beanName = BeanFactoryUtils.transformedBeanName(beanNameIncludingFactory);
143146
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
144147
}
145148
else {
@@ -151,6 +154,7 @@ else if (enforceExistingDefinition) {
151154
throw new IllegalStateException("Unable to override bean '" + beanName + "': there is no " +
152155
"bean definition to replace with that name of type " + overrideMetadata.getBeanType());
153156
}
157+
beanNameIncludingFactory = beanName;
154158
}
155159

156160
if (existingBeanDefinition != null) {
@@ -160,15 +164,15 @@ else if (enforceExistingDefinition) {
160164
registry.registerBeanDefinition(beanName, beanDefinition);
161165

162166
Object override = overrideMetadata.createOverride(beanName, existingBeanDefinition, null);
163-
if (beanFactory.isSingleton(beanName)) {
167+
if (beanFactory.isSingleton(beanNameIncludingFactory)) {
164168
// Now we have an instance (the override) that we can register.
165169
// At this stage we don't expect a singleton instance to be present,
166170
// and this call will throw if there is such an instance already.
167171
beanFactory.registerSingleton(beanName, override);
168172
}
169173

170174
overrideMetadata.track(override, beanFactory);
171-
this.overrideRegistrar.registerNameForMetadata(overrideMetadata, beanName);
175+
this.overrideRegistrar.registerNameForMetadata(overrideMetadata, beanNameIncludingFactory);
172176
}
173177

174178
/**
@@ -190,7 +194,7 @@ private void registerWrapBean(ConfigurableListableBeanFactory beanFactory, Overr
190194
" (as required by annotated field '" + field.getDeclaringClass().getSimpleName() +
191195
"." + field.getName() + "')" + (candidateCount > 0 ? ": " + candidateNames : ""));
192196
}
193-
beanName = candidateNames.iterator().next();
197+
beanName = BeanFactoryUtils.transformedBeanName(candidateNames.iterator().next());
194198
}
195199
else {
196200
Set<String> candidates = getExistingBeanNamesByType(beanFactory, metadata, false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.bean.override.mockito;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.beans.factory.FactoryBean;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.junit.jupiter.SpringExtension;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.BDDMockito.given;
31+
import static org.mockito.Mockito.mock;
32+
33+
/**
34+
* Test {@link MockitoBean @MockitoBean} for a factory bean.
35+
*
36+
* @author Phillip Webb
37+
*/
38+
@ExtendWith(SpringExtension.class)
39+
class MockitoBeanForBeanFactoryIntegrationTests {
40+
41+
// spring-boot/gh-7439
42+
43+
@MockitoBean
44+
private TestFactoryBean testFactoryBean;
45+
46+
@Autowired
47+
private ApplicationContext applicationContext;
48+
49+
@Test
50+
@SuppressWarnings({ "unchecked", "rawtypes" })
51+
void testName() {
52+
TestBean testBean = mock(TestBean.class);
53+
given(testBean.hello()).willReturn("amock");
54+
given(this.testFactoryBean.getObjectType()).willReturn((Class) TestBean.class);
55+
given(this.testFactoryBean.getObject()).willReturn(testBean);
56+
TestBean bean = this.applicationContext.getBean(TestBean.class);
57+
assertThat(bean.hello()).isEqualTo("amock");
58+
}
59+
60+
@Configuration(proxyBeanMethods = false)
61+
static class Config {
62+
63+
@Bean
64+
TestFactoryBean testFactoryBean() {
65+
return new TestFactoryBean();
66+
}
67+
68+
}
69+
70+
static class TestFactoryBean implements FactoryBean<TestBean> {
71+
72+
@Override
73+
public TestBean getObject() {
74+
return () -> "normal";
75+
}
76+
77+
@Override
78+
public Class<?> getObjectType() {
79+
return TestBean.class;
80+
}
81+
82+
@Override
83+
public boolean isSingleton() {
84+
return false;
85+
}
86+
87+
}
88+
89+
interface TestBean {
90+
91+
String hello();
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)