Skip to content

Commit 3654338

Browse files
committed
Improve diagnostics when a Bean Override cannot be selected by type
Close GH-34004
1 parent 15c6d34 commit 3654338

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
6767

6868
private static final BeanNameGenerator beanNameGenerator = DefaultBeanNameGenerator.INSTANCE;
6969

70+
private static final String unableToOverrideByTypeDiagnosticsMessage = " If the bean is defined from a @Bean method,"
71+
+ " please make sure the return type is the most specific type (recommended) or type can be assigned to %s.";
72+
7073
private final Set<BeanOverrideHandler> beanOverrideHandlers;
7174

7275
private final BeanOverrideRegistry beanOverrideRegistry;
@@ -154,7 +157,9 @@ else if (requireExistingBean) {
154157
Unable to replace bean: there is no bean with name '%s' and type %s \
155158
(as required by field '%s.%s')."""
156159
.formatted(beanName, handler.getBeanType(),
157-
field.getDeclaringClass().getSimpleName(), field.getName()));
160+
field.getDeclaringClass().getSimpleName(), field.getName())
161+
+ unableToOverrideByTypeDiagnosticsMessage.formatted(handler.getBeanType())
162+
);
158163
}
159164
}
160165

@@ -242,6 +247,7 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
242247
if (candidateCount == 0) {
243248
message += "there are no beans of type %s (as required by field '%s.%s')."
244249
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName());
250+
message += unableToOverrideByTypeDiagnosticsMessage.formatted(beanType);
245251
}
246252
else {
247253
message += "found %d beans of type %s (as required by field '%s.%s'): %s"
@@ -285,7 +291,9 @@ else if (candidateCount == 0) {
285291
if (requireExistingBean) {
286292
throw new IllegalStateException(
287293
"Unable to override bean: there are no beans of type %s (as required by field '%s.%s')."
288-
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName()));
294+
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName())
295+
+ unableToOverrideByTypeDiagnosticsMessage.formatted(beanType)
296+
);
289297
}
290298
return null;
291299
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void replaceBeanByNameWithoutMatchingBeanDefinitionFails() {
8585

8686
assertThatIllegalStateException()
8787
.isThrownBy(context::refresh)
88-
.withMessage("""
88+
.withMessageStartingWith("""
8989
Unable to replace bean: there is no bean with name 'descriptionBean' \
9090
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
9191
}
@@ -97,7 +97,7 @@ void replaceBeanByNameWithMatchingBeanDefinitionAndWrongTypeFails() {
9797

9898
assertThatIllegalStateException()
9999
.isThrownBy(context::refresh)
100-
.withMessage("""
100+
.withMessageStartingWith("""
101101
Unable to replace bean: there is no bean with name 'descriptionBean' \
102102
and type java.lang.String (as required by field 'ByNameTestCase.description').""");
103103
}
@@ -144,7 +144,7 @@ void replaceBeanByTypeWithoutMatchingBeanFails() {
144144

145145
assertThatIllegalStateException()
146146
.isThrownBy(context::refresh)
147-
.withMessage("""
147+
.withMessageStartingWith("""
148148
Unable to override bean: there are no beans of type java.lang.Integer \
149149
(as required by field 'ByTypeTestCase.counter').""");
150150
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
3939
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
4040
assertThatIllegalStateException()
4141
.isThrownBy(context::refresh)
42-
.withMessage("""
42+
.withMessageStartingWith("""
4343
Unable to replace bean: there is no bean with name 'beanToOverride' \
4444
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
4545
}
@@ -51,7 +51,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
5151
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
5252
assertThatIllegalStateException()
5353
.isThrownBy(context::refresh)
54-
.withMessage("""
54+
.withMessageStartingWith("""
5555
Unable to replace bean: there is no bean with name 'beanToOverride' \
5656
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
5757
}
@@ -62,7 +62,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
6262
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
6363
assertThatIllegalStateException()
6464
.isThrownBy(context::refresh)
65-
.withMessage("""
65+
.withMessageStartingWith("""
6666
Unable to override bean: there are no beans of \
6767
type %s (as required by field '%s.example').""".formatted(
6868
String.class.getName(), FailureByTypeLookup.class.getSimpleName()));

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
4040
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
4141
assertThatIllegalStateException()
4242
.isThrownBy(context::refresh)
43-
.withMessage("""
43+
.withMessageStartingWith("""
4444
Unable to replace bean: there is no bean with name 'beanToOverride' \
4545
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
4646
}
@@ -52,7 +52,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
5252
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
5353
assertThatIllegalStateException()
5454
.isThrownBy(context::refresh)
55-
.withMessage("""
55+
.withMessageStartingWith("""
5656
Unable to replace bean: there is no bean with name 'beanToOverride' \
5757
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
5858
}
@@ -63,7 +63,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
6363
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
6464
assertThatIllegalStateException()
6565
.isThrownBy(context::refresh)
66-
.withMessage("""
66+
.withMessageStartingWith("""
6767
Unable to override bean: there are no beans of \
6868
type java.lang.String (as required by field 'FailureByTypeLookup.example').""");
6969
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void contextCustomizerCannotBeCreatedWithNoSuchBeanType() {
5050
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(ByTypeSingleLookup.class, context);
5151
assertThatIllegalStateException()
5252
.isThrownBy(context::refresh)
53-
.withMessage("""
53+
.withMessageStartingWith("""
5454
Unable to select a bean to wrap: there are no beans of type java.lang.String \
5555
(as required by field 'ByTypeSingleLookup.example').""");
5656
}

0 commit comments

Comments
 (0)