Java 如何测试Spring缓存?
我正在尝试将缓存支持添加到我的项目中,以便在每次需要静态数据时缓存静态数据,而不联系数据库 我的Java 如何测试Spring缓存?,java,spring,unit-testing,caching,testing,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
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>
@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
端点来测试接收到的数据
问题但我如何测试缓存是否确实如预期的那样工作?有什么建议吗?您可以记录每个对象的创建。不应再次创建缓存对象
public Age(){
System.out.println("Age created"); // or use static int and count instances
}
另一个选项是创建自己的ConcurrentMapCacheFactoryBean。您可以在上找到实现
使用方面拦截存储库并计算调用次数。此特性只能在使用Spring Profile的测试中激活 e、 g 然后,您可以在计数器上断言值,以验证当您使用服务实例时,该值是否从缓存返回。可能重复的
public class MYConcurrentMapCacheFactoryBean implements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean{
// code from github
public String toString(){
// todo show content of ConcurrentMapCache
}
}
@Autowired
private ApplicationContext appContext;
public void printCacheContent(){
SimpleCacheManager cacheMng = (SimpleCacheManager) appContext.getBean("cacheManager");
System.out.println(cacheMng.loadCaches().toString());
}
@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();
}
}