day*_*mer 5 java testing spring caching unit-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>
Run Code Online (Sandbox Code Playgroud)
我的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();
}
}
Run Code Online (Sandbox Code Playgroud)
我有集成测试,该测试通过击中REST端点来接收数据。
问题
但是,如何测试缓存确实按预期工作?有什么建议吗?
您可以只记录每个对象的创建。不应再次创建缓存的对象。
public Age(){
System.out.println("Age created"); // or use static int and count instances
}
Run Code Online (Sandbox Code Playgroud)
另一个选择是创建您自己的ConcurrentMapCacheFactoryBean。你可以在github上找到实现
public class MYConcurrentMapCacheFactoryBean implements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean{
// code from github
public String toString(){
// todo show content of ConcurrentMapCache
}
}
Run Code Online (Sandbox Code Playgroud)
最后将 bean 定义更改applicationContext.xml为package.MYConcurrentMapCacheFactoryBean
我认为显示缓存内容的最佳方法是使用 ApplicationContext 获取缓存实例:
@Autowired
private ApplicationContext appContext;
public void printCacheContent(){
SimpleCacheManager cacheMng = (SimpleCacheManager) appContext.getBean("cacheManager");
System.out.println(cacheMng.loadCaches().toString());
}
Run Code Online (Sandbox Code Playgroud)