批注接口 MockBean


已过时, 待删除: 此 API 元素将从以后的版本中删除。
since 3.4.0 for removal in 4.0.0 in favor of MockitoBean
Annotation that can be used to add mocks to a Spring 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.

从以下版本开始:
1.4.0
作者:
Phillip Webb
另请参阅:
  • 可选元素概要

    可选元素
    修饰符和类型
    可选元素
    说明
    org.mockito.Answers
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    The Answers type to use on the mock.
    Class<?>[]
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    The classes to mock.
    Class<?>[]
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    Any extra interfaces that should also be declared on the mock.
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    The name of the bean to register or replace.
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    The reset mode to apply to the mock bean.
    boolean
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    If the generated mock is serializable.
    Class<?>[]
    已过时, 待删除: 此 API 元素将从以后的版本中删除。
    The classes to mock.
  • 元素详细资料

    • name

      String name
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      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.
      返回:
      the name of the bean
      默认值:
      ""
    • value

      @AliasFor("classes") Class<?>[] value
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      The classes to mock. This is an alias of classes() which can be used for brevity if no other attributes are defined. See classes() for details.
      返回:
      the classes to mock
      默认值:
      {}
    • classes

      @AliasFor("value") Class<?>[] classes
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      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 a name this attribute can only contain a single value.

      If this is the only specified attribute consider using the value alias instead.

      返回:
      the classes to mock
      默认值:
      {}
    • extraInterfaces

      Class<?>[] extraInterfaces
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      Any extra interfaces that should also be declared on the mock. See MockSettings.extraInterfaces(Class...) for details.
      返回:
      any extra interfaces
      默认值:
      {}
    • answer

      org.mockito.Answers answer
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      The Answers type to use on the mock.
      返回:
      the answer type
      默认值:
      RETURNS_DEFAULTS
    • serializable

      boolean serializable
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      If the generated mock is serializable. See MockSettings.serializable() for details.
      返回:
      if the mock is serializable
      默认值:
      false
    • reset

      MockReset reset
      已过时, 待删除: 此 API 元素将从以后的版本中删除。
      The reset mode to apply to the mock bean. The default is MockReset.AFTER meaning that mocks are automatically reset after each test method is invoked.
      返回:
      the reset mode
      默认值:
      AFTER