Annotation Interface SpykBean


Annotation that can be used to apply MockK spies 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.

Spies can be applied by type or by bean name. All beans in the context of a matching type (including subclasses) will be wrapped with the spy. 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 spied bean will be added to the context alongside the existing dependency.

When @SpykBean is used on a field, as well as being registered in the application context, the spy will also be injected into the field. Typical usage might be:

 @RunWith(SpringRunner::class)
 class ExampleTests {

     @SpykBean
     private lateinit var service: ExampleService;

     @Autowired
     private lateinit var userOfService UserOfService;

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

     @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)
 public class ExampleTests {

     @SpykBean
     @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 a @SpykBeans annotation.

See Also:
  • MockkPostProcessor
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<?>[]
    The classes to spy.
    com.ninjasquad.springmockk.MockkClear
    The reset mode to apply to the spied bean.
    The name of the bean to spy.
    Class<?>[]
    The classes to spy.
  • Element Details

    • name

      String name
      The name of the bean to spy. If not specified the name will either be generated or, if the spy is for an existing bean, the existing name will be used.
      Returns:
      the name of the bean
      Default:
      ""
    • value

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

      @AliasFor("value") Class<?>[] classes
      The classes to spy. Each class specified here will result in a spy being applied. Classes can be omitted when the annotation is used on a field.

      When @SpykBean 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 spy
      Default:
      {}
    • clear

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