Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diagnostics when a Bean Override cannot be selected by type #34006

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,

private static final BeanNameGenerator beanNameGenerator = DefaultBeanNameGenerator.INSTANCE;

private static final String unableToOverrideByTypeDiagnosticsMessage = " If the bean is defined from a @Bean method,"
+ " please make sure the return type is the most specific type (recommended) or type can be assigned to %s.";

private final Set<BeanOverrideHandler> beanOverrideHandlers;

private final BeanOverrideRegistry beanOverrideRegistry;
Expand Down Expand Up @@ -154,7 +157,9 @@ else if (requireExistingBean) {
Unable to replace bean: there is no bean with name '%s' and type %s \
(as required by field '%s.%s')."""
.formatted(beanName, handler.getBeanType(),
field.getDeclaringClass().getSimpleName(), field.getName()));
field.getDeclaringClass().getSimpleName(), field.getName())
+ unableToOverrideByTypeDiagnosticsMessage.formatted(handler.getBeanType())
);
}
}

Expand Down Expand Up @@ -242,6 +247,7 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
if (candidateCount == 0) {
message += "there are no beans of type %s (as required by field '%s.%s')."
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName());
message += unableToOverrideByTypeDiagnosticsMessage.formatted(beanType);
}
else {
message += "found %d beans of type %s (as required by field '%s.%s'): %s"
Expand Down Expand Up @@ -285,7 +291,9 @@ else if (candidateCount == 0) {
if (requireExistingBean) {
throw new IllegalStateException(
"Unable to override bean: there are no beans of type %s (as required by field '%s.%s')."
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName()));
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName())
+ unableToOverrideByTypeDiagnosticsMessage.formatted(beanType)
);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void replaceBeanByNameWithoutMatchingBeanDefinitionFails() {

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

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

assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of type java.lang.Integer \
(as required by field 'ByTypeTestCase.counter').""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -51,7 +51,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -62,7 +62,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of \
type %s (as required by field '%s.example').""".formatted(
String.class.getName(), FailureByTypeLookup.class.getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void cannotOverrideBeanByNameWithNoSuchBeanName() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -52,7 +52,7 @@ void cannotOverrideBeanByNameWithBeanOfWrongType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to replace bean: there is no bean with name 'beanToOverride' \
and type java.lang.String (as required by field 'FailureByNameLookup.example').""");
}
Expand All @@ -63,7 +63,7 @@ void cannotOverrideBeanByTypeWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to override bean: there are no beans of \
type java.lang.String (as required by field 'FailureByTypeLookup.example').""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void contextCustomizerCannotBeCreatedWithNoSuchBeanType() {
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(ByTypeSingleLookup.class, context);
assertThatIllegalStateException()
.isThrownBy(context::refresh)
.withMessage("""
.withMessageStartingWith("""
Unable to select a bean to wrap: there are no beans of type java.lang.String \
(as required by field 'ByTypeSingleLookup.example').""");
}
Expand Down
Loading