Annotation Interface MockitoBean


@Target(FIELD) @Retention(RUNTIME) @Documented public @interface MockitoBean
Mark a field to trigger a bean override using a Mockito mock.

If no explicit name() is specified, a target bean definition is selected according to the class of the annotated field, and there must be exactly one such candidate definition in the context. A @Qualifier annotation can be used to help disambiguate. If a name() is specified, either the definition exists in the application context and is replaced, or it doesn't and a new one is added to the context.

Dependencies that are known to the application context but are not beans (such as those registered directly) will not be found, and a mocked bean will be added to the context alongside the existing dependency.

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

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    org.mockito.Answers
    The Answers type to use on the mock.
    Class<?>[]
    Extra interfaces that should also be declared on the mock.
    The name of the bean to register or replace.
    The reset mode to apply to the mock.
    boolean
    Whether the generated mock is serializable.
  • Element Details

    • name

      String name
      The name of the bean to register or replace.

      If left unspecified, the bean to override is selected according to the annotated field's type.

      Returns:
      the name of the mocked bean
      Default:
      ""
    • extraInterfaces

      Class<?>[] extraInterfaces
      Extra interfaces that should also be declared on the mock.

      Defaults to none.

      Returns:
      any extra interfaces
      See Also:
      • MockSettings.extraInterfaces(Class...)
      Default:
      {}
    • answers

      org.mockito.Answers answers
      The Answers type to use on the mock.

      Defaults to Answers.RETURNS_DEFAULTS.

      Returns:
      the answer type
      Default:
      RETURNS_DEFAULTS
    • serializable

      boolean serializable
      Whether the generated mock is serializable.

      Defaults to false.

      Returns:
      true if the mock is serializable
      See Also:
      • MockSettings.serializable()
      Default:
      false
    • reset

      MockReset reset
      The reset mode to apply to the mock.

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

      Returns:
      the reset mode
      Default:
      AFTER