Annotation Interface MockitoSpyBean
@MockitoSpyBean is an annotation that can be applied to a non-static
field in a test class to override a bean in the test's
ApplicationContext
with a Mockito spy that wraps the original bean instance.
By default, the bean to spy is inferred from the type of the annotated
field. If multiple candidates exist, a @Qualifier annotation can be
used to help disambiguate. In the absence of a @Qualifier annotation,
the name of the annotated field will be used as a fallback qualifier.
Alternatively, you can explicitly specify a bean name to spy by setting the
value or name attribute. If a bean name is
specified, it is required that a target bean with that name has been previously
registered in the application context.
A spy cannot be created for components which are known to the application
context but are not beans — for example, components
registered directly as resolvable dependencies.
NOTE: Only singleton beans can be spied. Any attempt
to create a spy for a non-singleton bean will result in an exception. When
creating a spy for a FactoryBean,
a spy will be created for the object created by the FactoryBean, not
for the FactoryBean itself.
There are no restrictions on the visibility of a @MockitoSpyBean field.
Such fields can therefore be public, protected, package-private
(default visibility), or private depending on the needs or coding
practices of the project.
@MockitoSpyBean fields will be inherited from an enclosing test class by default.
See @NestedTestConfiguration
for details.
- Since:
- 6.2
- Author:
- Simon Baslé, Sam Brannen
- See Also:
-
Optional Element Summary
Optional Elements
-
Element Details
-
value
Alias forname().Intended to be used when no other attributes are needed — for example,
@MockitoSpyBean("customBeanName").- See Also:
- Default:
- ""
-
name
Name of the bean to spy.If left unspecified, the bean to spy is selected according to the annotated field's type, taking qualifiers into account if necessary. See the class-level documentation for details.
- See Also:
- Default:
- ""
-
reset
MockReset resetThe reset mode to apply to the spied bean.The default is
MockReset.AFTERmeaning that spies are automatically reset after each test method is invoked.- Returns:
- the reset mode
- Default:
- AFTER
-