【问题标题】:Spring 3.2 disable @Cacheable during unit testsSpring 3.2 在单元测试期间禁用 @Cacheable
【发布时间】:2014-02-24 18:29:15
【问题描述】:

我的单元测试遇到问题。我目前使用 ehcache 和 spring 3.2 (@Cacheable) 一切正常,但我想在单元测试期间禁用缓存。

所以我在 src/test/resources/ehcache.xml 中写道:

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

但缓存仍在工作!有人有想法吗?

提前感谢您的帮助!

【问题讨论】:

  • 您可以尝试从二级缓存中逐出作为一种选择。 (就在运行单元测试之前。)

标签: hibernate testng ehcache spring-data-jpa


【解决方案1】:

Spring 配置文件就是为此目的而设计的。请参阅文档herehere

在您的缓存管理器 bean (&lt;bean profile="test" ... /&gt;) 或上定义配置文件“测试”,并使用注释 @ActiveProfiles("test") 激活或不激活此配置文件。

如果您的问题仍然存在,请验证定义缓存的上下文是根上下文。

【讨论】:

  • 感谢您的回答,但问题是即使我没有在我的 applicationContextTest.xml 缓存中声明缓存管理器仍然启用,我也不知道禁用缓存的内容是什么
  • 对不起,我误会了。请问可以给spring context配置文件或者类和Entity类吗?
  • 要禁用 ehcache,只需添加此属性/值:net.sf.ehcache.disabled=true。您可以在 @BeforeClass 注释方法的代码中使用 System.setProperty("net.sf.ehcache.disabled", Boolean.TRUE.toString()); 执行此操作
  • 抱歉耽搁了,添加你的代码没有用,所以为了解决这个问题,我在适当的方法上添加了@CacheEvict。再次感谢您的帮助
猜你喜欢
  • 2021-03-05
  • 1970-01-01
  • 2017-01-30
  • 2013-06-25
  • 2012-10-25
  • 2012-06-06
  • 2011-11-18
  • 1970-01-01
  • 2014-02-28
相关资源
最近更新 更多