Skip to content

Commit 138f77a

Browse files
committed
Improve exception message if unable to select a bean to wrap for @MockitoSpyBean
See spring-projectsGH-33935
1 parent 64f93d5 commit 138f77a

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ private void wrapBean(ConfigurableListableBeanFactory beanFactory, BeanOverrideH
242242
if (candidateCount == 0) {
243243
message += "there are no beans of type %s (as required by field '%s.%s')."
244244
.formatted(beanType, field.getDeclaringClass().getSimpleName(), field.getName());
245+
message += " If the bean is defined from a @Bean method,"
246+
+ " please make sure the return type is the most specific type (recommended) or type can be assigned to %s."
247+
.formatted(beanType);
245248
}
246249
else {
247250
message += "found %d beans of type %s (as required by field '%s.%s'): %s"

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

Lines changed: 1 addition & 1 deletion
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)