-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
Using @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.