Annotation Interface MockkBean


Annotation that can be used to add MockK 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 run with 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 @MockkBean 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)
 class ExampleTests {

     @MockkBean
     private lateinit var service: ExampleService

     @Autowired
     private lateinit var userOfService: UserOfService

     @Test
     void testUserOfService() {
         every { service.greet() } returns "Hello"
         val actual = userOfService.makeUse()
         assertThat(actual).isEqualTo("Was: Hello")
     }

     @Configuration
     @Import(UserOfService::class) // A @Component injected with ExampleService
     class Config {
     }


 }
 
If there is more than one bean of the requested type, qualifier metadata must be specified at field level:
 @RunWith(SpringRunner.class)
 class ExampleTests {

     @MockkBean
     @Qualifier("example")
     private lateinit var service: ExampleService

     ...
 }
 

This annotation is @Repeatable and may be specified multiple times when working with Java 8 or contained within an @MockkBeans annotation.

See Also:
  • MockkPostProcessor
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<?>[]
    The classes to mock.
    com.ninjasquad.springmockk.MockkClear
    The clear mode to apply to the mock bean.
    Class<?>[]
    Any extra interfaces that should also be declared on the mock.
    The name of the bean to register or replace.
    boolean
    Specifies if the created mock will be relaxed or not
    boolean
    Specifies if the created mock will have relaxed Unit-returning functions
    Class<?>[]
    The classes to mock.
  • Element Details

    • name

      String name
      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

      @AliasFor("classes") Class<?>[] value
      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.
      Returns:
      the classes to mock
      Default:
      {}
    • classes

      @AliasFor("value") Class<?>[] classes
      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 @MockkBean 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.

      Returns:
      the classes to mock
      Default:
      {}
    • extraInterfaces

      Class<?>[] extraInterfaces
      Any extra interfaces that should also be declared on the mock.
      Returns:
      any extra interfaces
      Default:
      {}
    • clear

      com.ninjasquad.springmockk.MockkClear clear
      The clear mode to apply to the mock bean. The default is MockkClear.AFTER meaning that mocks are automatically reset after each test method is invoked.
      Returns:
      the clear mode
      Default:
      AFTER
    • relaxed

      boolean relaxed
      Specifies if the created mock will be relaxed or not
      Returns:
      true if relaxed, false otherwise
      Default:
      false
    • relaxUnitFun

      boolean relaxUnitFun
      Specifies if the created mock will have relaxed Unit-returning functions
      Returns:
      true if relaxed, false otherwise
      Default:
      false