Annotation Interface MockBean
ApplicationContext
. Can be
used as a class level annotation or on fields in either @Configuration
classes,
or test classes that are @RunWith
the SpringRunner
.
Mocks can be registered by type or by bean name
. When registered by
type, any existing single bean of a matching type (including subclasses) in the context
will be replaced by the mock. When registered by name, an existing bean can be
specifically targeted for replacement by a mock. In either case, if no existing bean is
defined a new one will be added. 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.
When @MockBean
is used on a field, as well as being registered in the
application context, the mock will also be injected into the field. Typical usage might
be:
@RunWith(SpringRunner.class) public class ExampleTests { @MockBean private ExampleService service; @Autowired private UserOfService userOfService; @Test public void testUserOfService() { given(this.service.greet()).willReturn("Hello"); String actual = this.userOfService.makeUse(); assertEquals("Was: Hello", actual); } @Configuration @Import(UserOfService.class) // A @Component injected with ExampleService static class Config { } }If there is more than one bean of the requested type, qualifier metadata must be specified at field level:
@RunWith(SpringRunner.class) public class ExampleTests { @MockBean @Qualifier("example") private ExampleService service; ... }
This annotation is @Repeatable
and may be specified multiple times when working
with Java 8 or contained within an @MockBeans
annotation.
- Since:
- 1.4.0
- Author:
- Phillip Webb
- See Also:
-
Optional Element Summary
Modifier and TypeOptional ElementDescriptionorg.mockito.Answers
Deprecated, for removal: This API element is subject to removal in a future version.TheAnswers
type to use on the mock.Class<?>[]
Deprecated, for removal: This API element is subject to removal in a future version.The classes to mock.Class<?>[]
Deprecated, for removal: This API element is subject to removal in a future version.Any extra interfaces that should also be declared on the mock.Deprecated, for removal: This API element is subject to removal in a future version.The name of the bean to register or replace.Deprecated, for removal: This API element is subject to removal in a future version.The reset mode to apply to the mock bean.boolean
Deprecated, for removal: This API element is subject to removal in a future version.If the generated mock is serializable.Class<?>[]
Deprecated, for removal: This API element is subject to removal in a future version.The classes to mock.
-
Element Details
-
name
String nameDeprecated, for removal: This API element is subject to removal in a future version.The name of the bean to register or replace. If not specified the name will either be generated or, if the mock replaces an existing bean, the existing name will be used.- Returns:
- the name of the bean
- Default:
- ""
-
value
Deprecated, for removal: This API element is subject to removal in a future version.The classes to mock. This is an alias ofclasses()
which can be used for brevity if no other attributes are defined. Seeclasses()
for details.- Returns:
- the classes to mock
- Default:
- {}
-
classes
Deprecated, for removal: This API element is subject to removal in a future version.The classes to mock. Each class specified here will result in a mock being created and registered with the application context. Classes can be omitted when the annotation is used on a field.When
@MockBean
also defines aname
this attribute can only contain a single value.If this is the only specified attribute consider using the
value
alias instead.- Returns:
- the classes to mock
- Default:
- {}
-
extraInterfaces
Class<?>[] extraInterfacesDeprecated, for removal: This API element is subject to removal in a future version.Any extra interfaces that should also be declared on the mock. SeeMockSettings.extraInterfaces(Class...)
for details.- Returns:
- any extra interfaces
- Default:
- {}
-
answer
org.mockito.Answers answerDeprecated, for removal: This API element is subject to removal in a future version.TheAnswers
type to use on the mock.- Returns:
- the answer type
- Default:
- RETURNS_DEFAULTS
-
serializable
boolean serializableDeprecated, for removal: This API element is subject to removal in a future version.If the generated mock is serializable. SeeMockSettings.serializable()
for details.- Returns:
- if the mock is serializable
- Default:
- false
-
reset
MockReset resetDeprecated, for removal: This API element is subject to removal in a future version.The reset mode to apply to the mock bean. The default isMockReset.AFTER
meaning that mocks are automatically reset after each test method is invoked.- Returns:
- the reset mode
- Default:
- AFTER
-
MockitoBean