Annotation Interface MockitoSpyBean


@Target({FIELD,TYPE}) @Retention(RUNTIME) @Documented @Repeatable(MockitoSpyBeans.class) @BeanOverride(org.springframework.test.context.bean.override.mockito.MockitoBeanOverrideProcessor.class) public @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 @Nested test class or in any class in the type hierarchy or enclosing class hierarchy above the @Nested test 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 @Nested test class or on any class or interface in the type hierarchy or enclosing class hierarchy above the @Nested test 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.

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: 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 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:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The name of the context hierarchy level in which this @MockitoSpyBean should be applied.
    Name of the bean to spy.
    The reset mode to apply to the spied bean.
    Class<?>[]
    One or more types to spy.
    Alias for name.
  • Element Details

    • value

      @AliasFor("name") String value
      Alias for name.

      Intended to be used when no other attributes are needed — for example, @MockitoSpyBean("customBeanName").

      See Also:
      Default:
      ""
    • name

      @AliasFor("value") String name
      Name of the bean to spy.

      If left unspecified, the bean to spy is selected according to the configured types or the annotated field's type, taking qualifiers into account if necessary. See the class-level documentation for details.

      See Also:
      Default:
      ""
    • types

      Class<?>[] types
      One 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 @MockitoSpyBean also defines a name, this attribute can only contain a single value.

      Returns:
      the types to spy
      Since:
      6.2.3
      Default:
      {}
    • contextName

      String contextName
      The name of the context hierarchy level in which this @MockitoSpyBean should be applied.

      Defaults to an empty string which indicates that this @MockitoSpyBean should 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:
      ""
    • reset

      MockReset reset
      The reset mode to apply to the spied bean.

      The default is MockReset.AFTER meaning that spies are automatically reset after each test method is invoked.

      Returns:
      the reset mode
      Default:
      AFTER