spring testing: Another CacheManager with same name 'myCacheManager' already exists in the same VM

Add @DirtiesContext annotation to your test class:

@ContextConfiguration(...)
@RunWith(...)
@DirtiesContext // <== add e.g. on class level
public class MyTest {
    // ...
}

This annotation indicates that the application context associated with a test is dirty and should be closed. Subsequent tests will be supplied a new context. Works on class-level and method-level.


I don't know if the question/issue are still relevent, but here's a simple/proper solution (Don't need to add @DirtiesContext in all your tests). Avoid @DirtiesContext allows you to have only one shared context for all integration tests (via run by maven for example, or run all tests in an IDE). That avoids multiple problems caused by multiple contexts started in same times.

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  p:configLocation="ehcache.xml"
  p:cacheManagerName="myCacheManager"
  p:shared="${ehcacheManager.shared:true}"
  p:acceptExisting:"${ehcacheManager.acceptExisting:false}"/>

In your tests (integration tests), set those properties

ehcacheManager.acceptExisting=true
ehcacheManager.shared=false

It allows Spring to create an EhcacheManager (ehcache) for each test, but if an EhcacheManager with the same name exist, Spring will just reuse it. And Spring will also not destroy/shutdown it in the context annotated with @DirtiesContext.

The idea is simple, you prevent the destroy of EhcacheManager when using @DirtiesContext.

It's applicable if you use Spring 4 and EhCache:2.5+. With Spring 3, you must an extends of EhCacheManagerFactoryBean to add these two properties.

Don't forget to clear your cache before each test :)


You can run your tests with caching disabled even if your code has methods with @Cacheable annotations.

That way you do not have to slow your test run down by marking all of your tests with @DirtiesContext.

Put the cache related Spring configurations in their own Spring config file, eg. applicationContext-cache.xml file.

Include that applicationContext-cache.xml file only when running the application live, but not in your tests.

If you specifically want to test caching, then you need the @DirtiesContext annotation.