Annotation Interface MockitoBean


@Target(FIELD) @Retention(RUNTIME) @Documented @BeanOverride(org.springframework.test.context.bean.override.mockito.MockitoBeanOverrideProcessor.class) public @interface MockitoBean
@MockitoBean is an annotation that can be applied to a field in a test class to override a bean in the test's ApplicationContext using a Mockito mock.

By default, the bean to mock 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 mock by setting the value or name attribute.

A new bean definition will be created if a corresponding bean definition does not exist. However, if you would like for the test to fail when a corresponding bean definition does not exist, you can set the enforceOverride attribute to true.

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.

NOTE: Only singleton beans can be overridden. Any attempt to mock a non-singleton bean will result in an exception.

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

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    org.mockito.Answers
    The Answers type to use in the mock.
    boolean
    Whether to require the existence of a bean definition for the bean being overridden.
    Class<?>[]
    Extra interfaces that should also be declared by the mock.
    Name of the bean to mock.
    The reset mode to apply to the mock.
    boolean
    Whether the generated mock is serializable.
    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, @MockitoBean("customBeanName").

      See Also:
      Default:
      ""
    • name

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

      If left unspecified, the bean to mock 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:
      ""
    • extraInterfaces

      Class<?>[] extraInterfaces
      Extra interfaces that should also be declared by 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 in 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
    • enforceOverride

      boolean enforceOverride
      Whether to require the existence of a bean definition for the bean being overridden.

      Defaults to false which means that a new bean definition will be created if a corresponding bean definition does not exist.

      Set to true to cause an exception to be thrown if a corresponding bean definition does not exist.

      See Also:
      Default:
      false