Skip to content

Commit aa7b459

Browse files
committed
Fix Phantom Read problem for Bean Overrides in the TestContext framework
To make an analogy to read phenomena for transactional databases, this commit effectively fixes the "Phantom Read" problem for Bean Overrides. A phantom read occurs when the BeanOverrideBeanFactoryPostProcessor retrieves a set of bean names by-type twice and a new bean definition for a compatible type has been created in the BeanFactory by a BeanOverrideHandler between the first and second retrieval. Continue reading for the details... Prior to this commit, the injection of test Bean Overrides (for example, when using @⁠MockitoBean) could fail in certain scenarios if overrides were created for nonexistent beans "by type" without an explicit name or qualifier. Specifically, if an override for a SubType was created first, and subsequently an attempt was made to create an override for a SuperType (where SubType extends SuperType), the override for the SuperType would "override the override" for the SubType, effectively removing the override for the SubType. Consequently, injection of the override instance into the SubType field would fail with an error message similar to the following. BeanNotOfRequiredTypeException: Bean named 'Subtype#0' is expected to be of type 'Subtype' but was actually of type 'Supertype$Mock$XHb7Aspo' This commit addresses this issue by tracking all generated bean names (in a generatedBeanNames set) and ensuring that a new bean override instance is created for the current BeanOverrideHandler if a previous BeanOverrideHandler already created a bean override instance that now matches the type required by the current BeanOverrideHandler. In other words, if the generatedBeanNames set already contains the beanName that we just found by-type, we cannot "override the override", because we would lose one of the overrides. Instead, we must create a new override for the current handler. In the example given above, we must end up with overrides for both SuperType and SubType. Closes gh-34025
1 parent 03fe1f0 commit aa7b459

File tree

4 files changed

+149
-8
lines changed

4 files changed

+149
-8
lines changed

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

+19-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Field;
2020
import java.util.Arrays;
21+
import java.util.HashSet;
2122
import java.util.LinkedHashSet;
2223
import java.util.Set;
2324

@@ -93,12 +94,15 @@ public int getOrder() {
9394

9495
@Override
9596
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
97+
Set<String> generatedBeanNames = new HashSet<>();
9698
for (BeanOverrideHandler handler : this.beanOverrideHandlers) {
97-
registerBeanOverride(beanFactory, handler);
99+
registerBeanOverride(beanFactory, handler, generatedBeanNames);
98100
}
99101
}
100102

101-
private void registerBeanOverride(ConfigurableListableBeanFactory beanFactory, BeanOverrideHandler handler) {
103+
private void registerBeanOverride(ConfigurableListableBeanFactory beanFactory, BeanOverrideHandler handler,
104+
Set<String> generatedBeanNames) {
105+
102106
String beanName = handler.getBeanName();
103107
Field field = handler.getField();
104108
Assert.state(!BeanFactoryUtils.isFactoryDereference(beanName),() -> """
@@ -107,14 +111,14 @@ private void registerBeanOverride(ConfigurableListableBeanFactory beanFactory, B
107111
beanName, field.getDeclaringClass().getSimpleName(), field.getName()));
108112

109113
switch (handler.getStrategy()) {
110-
case REPLACE -> replaceOrCreateBean(beanFactory, handler, true);
111-
case REPLACE_OR_CREATE -> replaceOrCreateBean(beanFactory, handler, false);
114+
case REPLACE -> replaceOrCreateBean(beanFactory, handler, generatedBeanNames, true);
115+
case REPLACE_OR_CREATE -> replaceOrCreateBean(beanFactory, handler, generatedBeanNames, false);
112116
case WRAP -> wrapBean(beanFactory, handler);
113117
}
114118
}
115119

116120
private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideHandler handler,
117-
boolean requireExistingBean) {
121+
Set<String> generatedBeanNames, boolean requireExistingBean) {
118122

119123
// NOTE: This method supports 3 distinct scenarios which must be accounted for.
120124
//
@@ -134,7 +138,15 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
134138
BeanDefinition existingBeanDefinition = null;
135139
if (beanName == null) {
136140
beanName = getBeanNameForType(beanFactory, handler, requireExistingBean);
137-
if (beanName != null) {
141+
// If the generatedBeanNames set already contains the beanName that we
142+
// just found by-type, that means we are experiencing a "phantom read"
143+
// (i.e., we found a bean that was not previously there). Consequently,
144+
// we cannot "override the override", because we would lose one of the
145+
// overrides. Instead, we must create a new override for the current
146+
// handler. For example, if one handler creates an override for a SubType
147+
// and a subsequent handler creates an override for a SuperType of that
148+
// SubType, we must end up with overrides for both SuperType and SubType.
149+
if (beanName != null && !generatedBeanNames.contains(beanName)) {
138150
// 1) We are overriding an existing bean by-type.
139151
beanName = BeanFactoryUtils.transformedBeanName(beanName);
140152
// If we are overriding a manually registered singleton, we won't find
@@ -197,6 +209,7 @@ else if (Boolean.getBoolean(AbstractAotProcessor.AOT_PROCESSING)) {
197209
// Generate a name for the nonexistent bean.
198210
if (PSEUDO_BEAN_NAME_PLACEHOLDER.equals(beanName)) {
199211
beanName = beanNameGenerator.generateBeanName(pseudoBeanDefinition, registry);
212+
generatedBeanNames.add(beanName);
200213
}
201214

202215
registry.registerBeanDefinition(beanName, pseudoBeanDefinition);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.test.context.bean.override;
1818

19-
import java.util.HashSet;
19+
import java.util.LinkedHashSet;
2020
import java.util.List;
2121
import java.util.Set;
2222

@@ -42,7 +42,7 @@ class BeanOverrideContextCustomizerFactory implements ContextCustomizerFactory {
4242
public BeanOverrideContextCustomizer createContextCustomizer(Class<?> testClass,
4343
List<ContextConfigurationAttributes> configAttributes) {
4444

45-
Set<BeanOverrideHandler> handlers = new HashSet<>();
45+
Set<BeanOverrideHandler> handlers = new LinkedHashSet<>();
4646
findBeanOverrideHandler(testClass, handlers);
4747
if (handlers.isEmpty()) {
4848
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.bean.override.example.ExampleService;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Integration tests for {@link MockitoBean @MockitoBean} where duplicate mocks
31+
* are created for the same nonexistent type.
32+
*
33+
* @author Sam Brannen
34+
* @since 6.2.1
35+
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a>
36+
*/
37+
@SpringJUnitConfig
38+
public class MockitoBeanDuplicateTypeIntegrationTests {
39+
40+
@MockitoBean
41+
ExampleService service1;
42+
43+
@MockitoBean
44+
ExampleService service2;
45+
46+
@Autowired
47+
List<ExampleService> services;
48+
49+
50+
@Test
51+
void duplicateMocksShouldHaveBeenCreated() {
52+
assertThat(service1).isNotSameAs(service2);
53+
assertThat(services).hasSize(2);
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 java.util.List;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Integration tests for {@link MockitoBean @MockitoBean} where mocks are created
30+
* for nonexistent beans for a supertype and subtype of that supertype.
31+
*
32+
* <p>This test class is designed to reproduce scenarios that previously failed
33+
* along the lines of the following.
34+
*
35+
* <p>BeanNotOfRequiredTypeException: Bean named 'Subtype#0' is expected to be
36+
* of type 'Subtype' but was actually of type 'Supertype$MockitoMock$XHb7Aspo'
37+
*
38+
* @author Sam Brannen
39+
* @since 6.2.1
40+
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a>
41+
*/
42+
@SpringJUnitConfig
43+
public class MockitoBeanSuperAndSubtypeIntegrationTests {
44+
45+
// The declaration order of the following fields is intentional, and prior
46+
// to fixing gh-34025 this test class consistently failed on JDK 17.
47+
48+
@MockitoBean
49+
Subtype subtype;
50+
51+
@MockitoBean
52+
Supertype supertype;
53+
54+
55+
@Autowired
56+
List<Supertype> supertypes;
57+
58+
59+
@Test
60+
void bothMocksShouldHaveBeenCreated() {
61+
assertThat(supertype).isNotSameAs(subtype);
62+
assertThat(supertypes).hasSize(2);
63+
}
64+
65+
66+
interface Supertype {
67+
}
68+
69+
interface Subtype extends Supertype {
70+
}
71+
72+
}

0 commit comments

Comments
 (0)