我遇到了以下问题。
我的项目中有一套测试服,每个测试都运行良好。
然而,当我将它们作为套件运行时,我们中的一些会因以下异常而失败:
Caused by: java.lang.IllegalStateException: The dao Cache is not alive (STATUS_SHUTDOWN)
at net.sf.ehcache.Cache$CacheStatus.checkAlive(Cache.java:4269)
at net.sf.ehcache.Cache.checkStatus(Cache.java:2703)
at net.sf.ehcache.Cache.get(Cache.java:1576)
at org.springframework.cache.ehcache.EhCacheCache.get(EhCacheCache.java:61)
at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:310)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:198)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
有没有办法避免这种行为,即在多次测试中保持缓存活着还是正确关闭?
答案 0 :(得分:4)
尝试在测试环境中的EhCacheManagerFactoryBean
或EhCacheCacheManager
中将共享属性设置为false。
答案 1 :(得分:2)
仅为测试创建单独的缓存配置!并放置范围"原型"
@Configuration
@EnableCaching
public class EhCacheConfig {
@Bean(name = "cacheManager")
@Scope("prototype")
public CacheManager getCacheManager() {
return new EhCacheCacheManager(getEhCacheFactory().getObject());
}
@Bean
@Scope("prototype")
public EhCacheManagerFactoryBean getEhCacheFactory() {
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return factoryBean;
}
}
答案 2 :(得分:0)
JUnit分享Spring的速度上下文。 在我的一个测试中明确删除Spring上下文关闭时,我避免了这个异常。见Reuse spring application context across junit test classes