Skip to content

Commit e6f2f86

Browse files
committed
Merge branch '6.2.x'
2 parents 1a29fbd + d59991f commit e6f2f86

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

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

+18-7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Simon Baslé
6161
* @author Stephane Nicoll
6262
* @author Sam Brannen
63+
* @author Yanming Zhou
6364
* @since 6.2
6465
*/
6566
class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
@@ -169,8 +170,10 @@ private void replaceOrCreateBean(ConfigurableListableBeanFactory beanFactory, Be
169170
}
170171
else if (requireExistingBean) {
171172
Field field = handler.getField();
172-
throw new IllegalStateException(
173-
"Unable to replace bean: there is no bean with name '%s' and type %s%s."
173+
throw new IllegalStateException("""
174+
Unable to replace bean: there is no bean with name '%s' and type %s%s. \
175+
If the bean is defined in a @Bean method, make sure the return type is the \
176+
most specific type possible (for example, the concrete implementation type)."""
174177
.formatted(beanName, handler.getBeanType(), requiredByField(field)));
175178
}
176179
// 4) We are creating a bean by-name with the provided beanName.
@@ -258,7 +261,11 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
258261
String message = "Unable to select a bean to wrap: ";
259262
int candidateCount = candidateNames.size();
260263
if (candidateCount == 0) {
261-
message += "there are no beans of type %s%s.".formatted(beanType, requiredByField(field));
264+
message += """
265+
there are no beans of type %s%s. \
266+
If the bean is defined in a @Bean method, make sure the return type is the \
267+
most specific type possible (for example, the concrete implementation type)."""
268+
.formatted(beanType, requiredByField(field));
262269
}
263270
else {
264271
message += "found %d beans of type %s%s: %s"
@@ -272,8 +279,10 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
272279
// We are wrapping an existing bean by-name.
273280
Set<String> candidates = getExistingBeanNamesByType(beanFactory, handler, false);
274281
if (!candidates.contains(beanName)) {
275-
throw new IllegalStateException(
276-
"Unable to wrap bean: there is no bean with name '%s' and type %s%s."
282+
throw new IllegalStateException("""
283+
Unable to wrap bean: there is no bean with name '%s' and type %s%s. \
284+
If the bean is defined in a @Bean method, make sure the return type is the \
285+
most specific type possible (for example, the concrete implementation type)."""
277286
.formatted(beanName, beanType, requiredByField(field)));
278287
}
279288
}
@@ -297,8 +306,10 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
297306
int candidateCount = candidateNames.size();
298307
if (candidateCount == 0) {
299308
if (requireExistingBean) {
300-
throw new IllegalStateException(
301-
"Unable to override bean: there are no beans of type %s%s."
309+
throw new IllegalStateException("""
310+
Unable to override bean: there are no beans of type %s%s. \
311+
If the bean is defined in a @Bean method, make sure the return type is the \
312+
most specific type possible (for example, the concrete implementation type)."""
302313
.formatted(beanType, requiredByField(field)));
303314
}
304315
return null;

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ void replaceBeanByNameWithoutMatchingBeanDefinitionFails() {
8787
.isThrownBy(context::refresh)
8888
.withMessage("""
8989
Unable to replace bean: there is no bean with name 'descriptionBean' \
90-
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
90+
and type java.lang.String (as required by field 'ByNameTestCase.description'). \
91+
If the bean is defined in a @Bean method, make sure the return type is the most \
92+
specific type possible (for example, the concrete implementation type).""");
9193
}
9294

9395
@Test
@@ -99,7 +101,9 @@ void replaceBeanByNameWithMatchingBeanDefinitionAndWrongTypeFails() {
99101
.isThrownBy(context::refresh)
100102
.withMessage("""
101103
Unable to replace bean: there is no bean with name 'descriptionBean' \
102-
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
104+
and type java.lang.String (as required by field 'ByNameTestCase.description'). \
105+
If the bean is defined in a @Bean method, make sure the return type is the most \
106+
specific type possible (for example, the concrete implementation type).""");
103107
}
104108

105109
@Test
@@ -146,7 +150,9 @@ void replaceBeanByTypeWithoutMatchingBeanFails() {
146150
.isThrownBy(context::refresh)
147151
.withMessage("""
148152
Unable to override bean: there are no beans of type java.lang.Integer \
149-
(as required by field 'ByTypeTestCase.counter').""");
153+
(as required by field 'ByTypeTestCase.counter'). \
154+
If the bean is defined in a @Bean method, make sure the return type is the most \
155+
specific type possible (for example, the concrete implementation type).""");
150156
}
151157

152158
@Test

spring-test/src/test/java/org/springframework/test/context/bean/override/convention/TestBeanTests.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
4242
.isThrownBy(context::refresh)
4343
.withMessage("""
4444
Unable to replace bean: there is no bean with name 'beanToOverride' \
45-
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
45+
and type java.lang.String (as required by field 'FailureByNameLookup.example'). \
46+
If the bean is defined in a @Bean method, make sure the return type is the most \
47+
specific type possible (for example, the concrete implementation type).""");
4648
}
4749

4850
@Test
@@ -54,7 +56,9 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
5456
.isThrownBy(context::refresh)
5557
.withMessage("""
5658
Unable to replace bean: there is no bean with name 'beanToOverride' \
57-
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
59+
and type java.lang.String (as required by field 'FailureByNameLookup.example'). \
60+
If the bean is defined in a @Bean method, make sure the return type is the most \
61+
specific type possible (for example, the concrete implementation type).""");
5862
}
5963

6064
@Test
@@ -64,8 +68,9 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
6468
assertThatIllegalStateException()
6569
.isThrownBy(context::refresh)
6670
.withMessage("""
67-
Unable to override bean: there are no beans of \
68-
type %s (as required by field '%s.example').""",
71+
Unable to override bean: there are no beans of type %s (as required by field '%s.example'). \
72+
If the bean is defined in a @Bean method, make sure the return type is the most \
73+
specific type possible (for example, the concrete implementation type).""",
6974
String.class.getName(), FailureByTypeLookup.class.getSimpleName());
7075
}
7176

spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanConfigurationErrorTests.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,7 +42,9 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
4242
.isThrownBy(context::refresh)
4343
.withMessage("""
4444
Unable to replace bean: there is no bean with name 'beanToOverride' \
45-
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
45+
and type java.lang.String (as required by field 'FailureByNameLookup.example'). \
46+
If the bean is defined in a @Bean method, make sure the return type is the most \
47+
specific type possible (for example, the concrete implementation type).""");
4648
}
4749

4850
@Test
@@ -54,7 +56,9 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
5456
.isThrownBy(context::refresh)
5557
.withMessage("""
5658
Unable to replace bean: there is no bean with name 'beanToOverride' \
57-
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
59+
and type java.lang.String (as required by field 'FailureByNameLookup.example'). \
60+
If the bean is defined in a @Bean method, make sure the return type is the most \
61+
specific type possible (for example, the concrete implementation type).""");
5862
}
5963

6064
@Test
@@ -65,7 +69,9 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
6569
.isThrownBy(context::refresh)
6670
.withMessage("""
6771
Unable to override bean: there are no beans of \
68-
type java.lang.String (as required by field 'FailureByTypeLookup.example').""");
72+
type java.lang.String (as required by field 'FailureByTypeLookup.example'). \
73+
If the bean is defined in a @Bean method, make sure the return type is the most \
74+
specific type possible (for example, the concrete implementation type).""");
6975
}
7076

7177
@Test

spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanConfigurationErrorTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,9 @@ void contextCustomizerCannotBeCreatedWithNoSuchBeanName() {
4141
.isThrownBy(context::refresh)
4242
.withMessage("""
4343
Unable to wrap bean: there is no bean with name 'beanToSpy' and \
44-
type java.lang.String (as required by field 'ByNameSingleLookup.example').""");
44+
type java.lang.String (as required by field 'ByNameSingleLookup.example'). \
45+
If the bean is defined in a @Bean method, make sure the return type is the most \
46+
specific type possible (for example, the concrete implementation type).""");
4547
}
4648

4749
@Test
@@ -52,7 +54,9 @@ void contextCustomizerCannotBeCreatedWithNoSuchBeanType() {
5254
.isThrownBy(context::refresh)
5355
.withMessage("""
5456
Unable to select a bean to wrap: there are no beans of type java.lang.String \
55-
(as required by field 'ByTypeSingleLookup.example').""");
57+
(as required by field 'ByTypeSingleLookup.example'). \
58+
If the bean is defined in a @Bean method, make sure the return type is the most \
59+
specific type possible (for example, the concrete implementation type).""");
5660
}
5761

5862
@Test

0 commit comments

Comments
 (0)