Annotation Interface MockitoSpyBean


@Target(FIELD) @Retention(RUNTIME) @Documented @BeanOverride(org.springframework.test.context.bean.override.mockito.MockitoBeanOverrideProcessor.class) public @interface MockitoSpyBean
Mark a field to trigger a bean override using a Mockito spy, which will wrap 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.

Since:
6.2
Author:
Simon Baslé, Sam Brannen
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Name of the bean to spy.
    The reset mode to apply to the spied bean.
    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 annotated field's type, taking qualifiers into account if necessary. See the class-level documentation for details.

      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