问题描述
我正在尝试使用以下代码测试 @Cacheable 方法,但不起作用,该方法没有被缓存,如果不是将 @Cacheable 放在 getCountCache 方法中,而是将它放在 getCount 方法中进行测试有效,尝试了很多方法但找不到原因。
@ContextConfiguration
@ExtendWith(SpringExtension.class)
public class CacheTest {
static class MyRepoImpl {
private Count count;
public MyRepoImpl(Count count){
this.count = count;
}
public int getCount(String key){
return getCountCache(key);
}
@Cacheable(cacheNames = "sample")
public int getCountCache(String key) {
return count.getCount();
}
}
static class Count {
int i = 0;
public int getCount() {
return i++;
}
}
@EnableCaching
@Configuration
public static class Config {
@Bean CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
@Bean MyRepoImpl myRepo() {
count = Mockito.mock(Count.class);
return new MyRepoImpl(count);
}
}
static Count count;
@Autowired MyRepoImpl repo;
@Test
public void methodInvocationShouldBeCached() {
Mockito.when(count.getCount()).thenReturn(1,2,3,4);
Object result = repo.getCount("foo");
assertthat(result).isEqualTo(1);
Mockito.verify(count,Mockito.times(1)).getCount();
result = repo.getCount("foo");
assertthat(result).isEqualTo(1);
Mockito.verify(count,Mockito.times(1)).getCount();
}
}
解决方法
由于 Spring 缓存适用于使用从类内部调用的 @Cachable
注释的 bean 方法的代理,因此不会缓存。您需要直接从类外部调用它才能使缓存机制正常工作。
Spring cache @Cacheable method ignored when called from within the same class 这个解释真的很好。