Annotation 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 ElementsModifier and TypeOptional ElementDescriptionorg.mockito.AnswersTheAnswerstype to use in the mock.booleanWhether 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.booleanWhether the generated mock is serializable.Alias forname().
-
Element Details
-
value
Alias forname().Intended to be used when no other attributes are needed — for example,
@MockitoBean("customBeanName").- See Also:
- Default:
- ""
-
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<?>[] extraInterfacesExtra 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 answersTheAnswerstype to use in the mock.Defaults to
Answers.RETURNS_DEFAULTS.- Returns:
- the answer type
- Default:
- RETURNS_DEFAULTS
-
serializable
boolean serializableWhether the generated mock is serializable.Defaults to
false.- Returns:
trueif the mock is serializable- See Also:
-
MockSettings.serializable()
- Default:
- false
-
reset
MockReset resetThe reset mode to apply to the mock.The default is
MockReset.AFTERmeaning that mocks are automatically reset after each test method is invoked.- Returns:
- the reset mode
- Default:
- AFTER
-
enforceOverride
boolean enforceOverrideWhether to require the existence of a bean definition for the bean being overridden.Defaults to
falsewhich means that a new bean definition will be created if a corresponding bean definition does not exist.Set to
trueto cause an exception to be thrown if a corresponding bean definition does not exist.- Default:
- false
-