0

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!

Denis Cucchietti
  • 201
  • 6
  • 16
  • You may try to evict from the secondary cache as one option. (just before running your unit test. ) – Zeus Jan 31 '14 at 21:54

1 Answers1

0

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.

L. BIZE
  • 195
  • 2
  • 4
  • Thanks for your answer but the problem is that I don't know what write to disable the cache even if i don't declare cache manager in my applicationContextTest.xml cache still enabled – Denis Cucchietti Jan 31 '14 at 22:43
  • Sorry, i misunderstood. Could you give spring context configuration file or class and Entity class plesae ? – L. BIZE Feb 01 '14 at 08:11
  • To disable ehcache, just add this property/value : `net.sf.ehcache.disabled=true`. You can do this in code in `@BeforeClass` annotated method with `System.setProperty("net.sf.ehcache.disabled", Boolean.TRUE.toString());` – L. BIZE Feb 02 '14 at 08:58
  • Sorry for the delay, adding your code didn't work, so to resolve the problem i added @CacheEvict on appropriate methods. Thanks again for your help – Denis Cucchietti Feb 04 '14 at 14:51