注释类型 MockBean
@Target({TYPE,FIELD}) @Retention(RUNTIME) @Documented @Repeatable(MockBeans.class) public @interface MockBean
Annotation that can be used to add mocks to a SpringApplicationContext. Can be used as a class level annotation or on fields in either@Configurationclasses, or test classes that are@RunWiththeSpringRunner.Mocks can be registered by type or by
bean name. Any existing single bean of the same type defined in the context will be replaced by the mock. 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 thoseregistered directly) will not be found and a mocked bean will be added to the context alongside the existing dependency.When
@MockBeanis 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
@Repeatableand may be specified multiple times when working with Java 8 or contained within an@MockBeansannotation.- 从以下版本开始:
 - 1.4.0
 - 另请参阅:
 MockitoPostProcessor
可选元素概要
可选元素 修饰符和类型 可选元素 说明 org.mockito.AnswersanswerTheAnswerstype to use on the mock.Class<?>[]classesThe classes to mock.Class<?>[]extraInterfacesAny extra interfaces that should also be declared on the mock.StringnameThe name of the bean to register or replace.MockResetresetThe reset mode to apply to the mock bean.booleanserializableIf the generated mock is serializable.Class<?>[]valueThe classes to mock.
classes
@AliasFor("value") Class<?>[] classesThe 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
@MockBeanalso defines anamethis attribute can only contain a single value.If this is the only specified attribute consider using the
valuealias instead.- 返回:
 - the classes to mock
 
- 默认值:
 - {}
 
extraInterfaces
Class<?>[] extraInterfaces
Any extra interfaces that should also be declared on the mock. SeeMockSettings.extraInterfaces(Class...)for details.- 返回:
 - any extra interfaces
 
- 默认值:
 - {}
 
answer
org.mockito.Answers answer
TheAnswerstype to use on the mock.- 返回:
 - the answer type
 
- 默认值:
 - org.mockito.Answers.RETURNS_DEFAULTS
 
serializable
boolean serializable
If the generated mock is serializable. SeeMockSettings.serializable()for details.- 返回:
 - if the mock is serializable
 
- 默认值:
 - false
 
reset
MockReset reset
The reset mode to apply to the mock bean. The default isMockReset.AFTERmeaning that mocks are automatically reset after each test method is invoked.- 返回:
 - the reset mode
 
- 默认值:
 - org.springframework.boot.test.mock.mockito.MockReset.AFTER