diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc index 9b4a0b4cf30..2b072e8b0e5 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc @@ -102,6 +102,10 @@ will be replaced with a singleton mock of the type of object created by the `Fac Similarly, when using `@MockitoSpyBean` to create a spy for a `FactoryBean`, a spy will be created for the object created by the `FactoryBean`, not for the `FactoryBean` itself. + +Furthermore, `@MockitoSpyBean` cannot be used to spy on a scoped proxy — for example, a +bean annotated with `@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)`. Any attempt to do +so will fail with an exception. ==== [NOTE] diff --git a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBean.java b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBean.java index db885b12591..7ad92818a36 100644 --- a/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBean.java +++ b/spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBean.java @@ -94,6 +94,12 @@ import org.springframework.test.context.bean.override.BeanOverride; * be created for the object created by the {@code FactoryBean}, not for the * {@code FactoryBean} itself. * + *

WARNING: {@code @MockitoSpyBean} cannot be used to spy on + * a scoped proxy — for example, a bean annotated with + * {@link org.springframework.context.annotation.Scope + * @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)}. Any attempt to do so will + * fail with an exception. + * *

There are no restrictions on the visibility of a {@code @MockitoSpyBean} field. * Such fields can therefore be {@code public}, {@code protected}, package-private * (default visibility), or {@code private} depending on the needs or coding