Spring 3.2 disable @Cacheable during unit tests

2019-09-04 23:35发布

问题:

I'm facing a problem with my unit tests. I currently use ehcache whith spring 3.2 (@Cacheable) everything works well but i would like to disable the cache during my unit tests.

So in src/test/resources/ehcache.xml i wrote :

<cache name="myCache"
       maxElementsInMemory="1"
       eternal="false"
       timeToIdleSeconds="0"
       timeToLiveSeconds="0"
       overflowToDisk="true"
       maxElementsOnDisk="0"
       diskPersistent="false"
       diskExpiryThreadIntervalSeconds="0"
       memoryStoreEvictionPolicy="LRU"/>

but the cache still working ! Is anyone has an idea ?

Thanks in advance for your help!

回答1:

Spring profile is done for that purpose. See documentation here, here.

Define a profile "test" in your cache manager bean (<bean profile="test" ... />) or upper and activate or not this profile with annotation @ActiveProfiles("test").

If your problem persists, verify that the context which defines the cache is the root context.