Announcement Announcement Module
No announcement yet.
EhCacheManagerFactoryBean and JUnit tests Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • EhCacheManagerFactoryBean and JUnit tests

    I'm on Spring 3.1.1 and EhCache 2.6.0. When running my JUnit tests extending AbstractJUnit4SpringContextTests, the first test runs fine, but the second test fails with the following error:

    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.cache.ehcache.EhCacheFactoryB ean#1e34760' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Cannot replace It does not equal the incumbent cache.
    at AutowireCapableBeanFactory.initializeBean(Abstract
    at AutowireCapableBeanFactory.doCreateBean(AbstractAu
    at AutowireCapableBeanFactory.createBean(AbstractAuto
    at nitionValueResolver.resolveInnerBean(BeanDefinitio
    Caused by: net.sf.ehcache.CacheException: Cannot replace It does not equal the incumbent cache.
    at net.sf.ehcache.CacheManager.replaceCacheWithDecora tedCache(
    at org.springframework.cache.ehcache.EhCacheFactoryBe an.afterPropertiesSet(
    at AutowireCapableBeanFactory.invokeInitMethods(Abstr
    at AutowireCapableBeanFactory.initializeBean(Abstract

    The relevant bean in my app context is as follows:

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheMa nagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache-application.xml" />
    <property name="cacheManagerName" value="cacheManager" />
    <property name="shared" value="true" />

    For now, I have locally modified EhCacheManagerFactoryBean.afterPropertiesSet() to simply return immediately, if CacheManager.getCacheManager returns an existing cache manager of the same name.

    I'm sure there must be a better approach than resorting to this... when the app context shuts down between unit tests, should I be doing something to remove all caches from each cache manager? (evidently one of the previous caches is still there from the previous run).

  • #2
    In my case this error only occurs if I'm using the decoration feature of the EhCacheFactoryBean (e.g. set blocking to true). I'm doing the decoration on my own now (with net.sf.ehcache.constructs.blocking.BlockingCache) to avoid it.