如何测试Spring-Cache?

时间:2014-07-16 20:17:26

标签: java spring unit-testing caching testing

我正在尝试为项目添加缓存支持,以便缓存静态数据,并且每次需要静态数据时都不会联系数据库

我的applicationContext.xml看起来像

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
    <context:component-scan base-package="com.yahoo.comma"/>
    <bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
        <property name="dataSource" ref="dataSource"/>
        <property name="changeLog" value="classpath:liquibase/changelog.xml"/>
        <property name="defaultSchema" value="pryme"/>
    </bean>
    <cache:annotation-driven/>

    <!-- generic cache manager -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="targetingAttributes"/>
            </set>
        </property>
    </bean>
</beans>

我的AgeRepository课程似乎是

@Component
@Transactional
public class AgeRepositoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
    private AgeRepository ageRepository;

    @SuppressWarnings("UnusedDeclaration")
    public AgeRepositoryService() {
    }

    @Autowired
    public AgeRepositoryService(@Nonnull final AgeRepository ageRepository) {
        this.ageRepository = ageRepository;
    }

    @Nonnull
    public Age save(@Nonnull final Age age) {
        LOGGER.debug("adding age {}", age);
        return ageRepository.saveAndFlush(age);
    }

    @Cacheable("targetingAttributes")
    @Nonnull
    public List<Age> getAges() {
        return ageRepository.findAll();
    }
}

我有通过点击REST端点来测试接收数据的集成测试。

问题
但是我如何测试缓存确实按预期工作?任何建议?

2 个答案:

答案 0 :(得分:1)

您可以记录每个对象的创建。不应再次创建缓存对象。

public Age(){
    System.out.println("Age created"); // or use static int and count instances
}

另一个选择是创建自己的 ConcurrentMapCacheFactoryBean 。您可以在github

上找到实施
public class MYConcurrentMapCacheFactoryBean implements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean{

   // code from github
   public String toString(){
       // todo show content of ConcurrentMapCache 
   }
}

最后将applicationContext.xml中的bean定义更改为package.MYConcurrentMapCacheFactoryBean

我认为显示缓存内容的最佳方法是使用ApplicationContext获取缓存实例:

@Autowired
private ApplicationContext appContext;

public void printCacheContent(){

     SimpleCacheManager cacheMng = (SimpleCacheManager) appContext.getBean("cacheManager");
     System.out.println(cacheMng.loadCaches().toString());
}

答案 1 :(得分:-1)

使用Aspect拦截存储库并计算调用次数。只有使用Spring Profile的测试才能激活此方面。

e.g。

@Aspect
@Component
@Profile("intercept-age-repository")
public class AgeRepositoryInterceptor {
private AtomicInteger findAllCounter = new AtomicInteger(0);

@Around("execution(* com.package.AgeRepository.findAll(..))")
public Object findAllInterceptor(ProceedingJoinPoint originalMethod) throws Throwable {
    findAllCounter.incrementAndGet();
    return originalMethod.proceed();
}

public int getFindAllInvocationCount(){
    return findAllCounter.get();
}
}

然后,您可以在计数器值上断言,以验证在使用服务实例时是否从缓存返回值。