Annotation Interface MockitoSpyBean
@MockitoSpyBean is an annotation that can be used in test classes to
override a bean in the test's
ApplicationContext
with a Mockito spy that wraps the original bean instance.
@MockitoSpyBean can be applied in the following ways.
- On a non-static field in a test class or any of its superclasses.
- On a non-static field in an enclosing class for a @Nestedtest class or in any class in the type hierarchy or enclosing class hierarchy above the@Nestedtest class.
- At the type level on a test class or any superclass or implemented interface in the type hierarchy above the test class.
- At the type level on an enclosing class for a @Nestedtest class or on any class or interface in the type hierarchy or enclosing class hierarchy above the@Nestedtest class.
When @MockitoSpyBean is declared on a field, the bean to spy is
inferred from the type of the annotated field. If multiple candidates exist in
the ApplicationContext, a @Qualifier annotation can be declared
on the field 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.
When @MockitoSpyBean is declared at the type level, the type of bean
(or beans) to spy must be supplied via the types attribute.
If multiple candidates exist in the ApplicationContext, you can
explicitly specify a bean name to spy by setting the name
attribute. Note, however, that the types attribute must contain a
single type if an explicit bean name is configured.
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: As stated in the documentation for Mockito, there are
times when using Mockito.when() is inappropriate for stubbing a spy
— for example, if calling a real method on a spy results in undesired
side effects. To avoid such undesired side effects, consider using
Mockito.doReturn(...).when(spy)...,
Mockito.doThrow(...).when(spy)...,
Mockito.doNothing().when(spy)..., and
similar methods.
WARNING: Using @MockitoSpyBean in conjunction with
@ContextHierarchy can lead to undesirable results since each
@MockitoSpyBean will be applied to all context hierarchy levels by default.
To ensure that a particular @MockitoSpyBean is applied to a single context
hierarchy level, set the contextName to match a
configured @ContextConfiguration
name.
See the Javadoc for @ContextHierarchy
for further details and examples.
NOTE: When creating a spy for a non-singleton bean, the
corresponding bean definition will be converted to a singleton. Consequently,
if you create a spy for a prototype or scoped bean, the spy will be treated as
a singleton. Similarly, 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 and type-level @MockitoSpyBean declarations
will be inherited from an enclosing test class by default. See
@NestedTestConfiguration
for details.
@MockitoSpyBean may be used as a meta-annotation to create
custom composed annotations — for example, to define common spy
configuration in a single annotation that can be reused across a test suite.
@MockitoSpyBean can also be used as a repeatable
annotation at the type level — for example, to spy on several beans by
name.
- Since:
- 6.2
- Author:
- Simon Baslé, Sam Brannen
- See Also:
- 
Element Details- 
value
- 
nameName of the bean to spy.If left unspecified, the bean to spy is selected according to the configured typesor the annotated field's type, taking qualifiers into account if necessary. See the class-level documentation for details.- See Also:
 - Default:
- ""
 
- 
typesClass<?>[] typesOne or more types to spy.Defaults to none. Each type specified will result in a spy being created and registered with the ApplicationContext.Types must be omitted when the annotation is used on a field. When @MockitoSpyBeanalso defines aname, this attribute can only contain a single value.- Returns:
- the types to spy
- Since:
- 6.2.3
 - Default:
- {}
 
- 
contextNameString contextNameThe name of the context hierarchy level in which this@MockitoSpyBeanshould be applied.Defaults to an empty string which indicates that this @MockitoSpyBeanshould be applied to all application contexts.If a context name is configured, it must match a name configured via @ContextConfiguration(name=...).- Since:
- 6.2.6
- See Also:
 - Default:
- ""
 
- 
resetMockReset 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
 
 
-