微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring 声明式缓存测试仅在测试对 @Cacheable 方法的直接调用时才有效

如何解决Spring 声明式缓存测试仅在测试对 @Cacheable 方法的直接调用时才有效

我正在尝试使用以下代码测试 @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 这个解释真的很好。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。