Annotation Type SpyBean
@Target({TYPE,FIELD}) @Retention(RUNTIME) @Documented @Repeatable(SpyBeans.class) public @interface SpyBean
Annotation that can be used to apply Mockito spies to a SpringApplicationContext
. Can be used as a class level annotation or on fields in either@Configuration
classes, or test classes that are@RunWith
theSpringRunner
.Spies can be applied by type or by
bean name
. All beans in the context of the same type 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 thoseregistered directly
) will not be found and a spied bean will be added to the context alongside the existing dependency.When
@SpyBean
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) public class ExampleTests { @SpyBean private ExampleService service; @Autowired private UserOfService userOfService; @Test public void testUserOfService() { String actual = this.userOfService.makeUse(); assertEquals("Was: Hello", actual); verify(this.service).greet(); } @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 { @SpyBean @Qualifier("example") private ExampleService service; ... }
This annotation is
@Repeatable
and may be specified multiple times when working with Java 8 or contained within a@SpyBeans
annotation.- Since:
- 1.4.0
- See Also:
MockitoPostProcessor
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Class<?>[]
classes
The classes to spy.String
name
The name of the bean to spy.boolean
proxyTargetAware
Indicates that Mockito methods such asverify(mock)
should use thetarget
of AOP advised beans, rather than the proxy itself.MockReset
reset
The reset mode to apply to the spied bean.Class<?>[]
value
The classes to spy.
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
@SpyBean
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 spy
- Default:
- {}
reset
MockReset reset
The reset mode to apply to the spied bean. The default isMockReset.AFTER
meaning that spies are automatically reset after each test method is invoked.- Returns:
- the reset mode
- Default:
- org.springframework.boot.test.mock.mockito.MockReset.AFTER
proxyTargetAware
boolean proxyTargetAware
Indicates that Mockito methods such asverify(mock)
should use thetarget
of AOP advised beans, rather than the proxy itself. If set tofalse
you may need to use the result ofAopTestUtils.getUltimateTargetObject(...)
when calling Mockito methods.- Returns:
true
if the target of AOP advised beans is used orfalse
if the proxy is used directly
- Default:
- true