@SpyBean removes @Caching functionality
See original GitHub issueUsing @SpyBean to test the caching behavior of a repository implementation appears to be impossible or broken. The idea is that you inject a spy bean into your test, then call the @Cacheable method twice, and then verify with Mockito that this method was indeed called once.
Please have a look at the example project here, which illustrates the problem. Essentially, using the repository @SpyBean magically removes the caching, as the failing Mockito.verify illustrates. It appears that @SpyBean should be made aware of caching and respect the proxy which is setup for caching.
As I was also working on #11077 (not yet merged), I don’t know if such testing is actually supported. So I’m also open to new ideas how to achieve the desired test of caching annotations. I do not want to put the caching annotations on the interface (as for example proposed here), as I consider caching a responsibility of the implementation. This makes it impossible to use @MockBean.
My current workaround is to @MockBean the underlying storage of the repository (we have another abstraction layer in our project, which is not present in the example project above). Still I think using the proposed @SpyBean solution might lead to easier testing.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
I also ran into this same problem (Spring Boot 1.5.12.RELEASE). Any solution for it?
@wilkinsona Thanks a lot! Another reason to migrate our projects to Spring Boot 2.x