2013-02-22 27 views
4

在將其標記爲重複之前,請先閱讀問題。我已閱讀了有關此異常的所有內容,但它不能解決此問題。而且我確實得到了一個稍微不同的例外,例如Another CacheManager with same name 'myCacheManager' already exists而不是Another unnamed CacheManager already exists彈簧測試:在同一虛擬機中已經存在另一個具有相同名稱'myCacheManager'的CacheManager

Spring配置:

<cache:annotation-driven cache-manager="cacheManager"/> 

<bean id="cacheManager" 
     class="org.springframework.cache.ehcache.EhCacheCacheManager" 
     p:cacheManager-ref="ehcache"/> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
     p:configLocation="ehcache.xml" 
     p:cacheManagerName="myCacheManager" 
     p:shared="true"/> 

的Ehcache

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
     updateCheck="false" name="myCacheManager"> 

</ehcache> 

的問題是,我有1(在未來更多)測試安全測試類。這些類還加載SecurityContext.xml

所以大多數測試類有這樣的註解:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:ApplicationContext.xml") 

但造成問題的類:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { 
    "classpath:ApplicationContext.xml", 
    "classpath:SecurityContext.xml" 
}) 

它似乎因爲位置不同的上下文再次加載,但ehcacheManager仍然是以前的測試活動。

注意:只有在運行多個測試(例如,像clean + build)時纔會發生這種情況。單獨運行這個測試類完全正常。

這是什麼問題?我該如何解決它?

回答

5

添加@DirtiesContext註釋到您的測試類:

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

此註釋表明,與測試相關的應用程序上下文是髒,應該關閉。隨後的測試將提供一個新的上下文。適用於課堂級別和方法級別。

+0

問題是這種情況發生在測試運行之前,而不是之前。或者說,否則我將不得不用這個註釋標記每一個測試類,以使其工作。我需要的是類似@RunInOwnContext的東西。 – 2013-02-23 13:30:45

+0

是的,這確實意味着你必須註釋每個測試課程......不幸的是,我沒有其他方式知道。 – jeha 2013-02-23 14:06:21

+0

當然,你試圖以你需要的方式修改SpringJUnit4ClassRunner/TestContextManager ...但我不知道這是你真正想要的 – jeha 2013-02-23 14:15:51

2

您可以禁用緩存運行測試即使您的代碼具有帶@Cacheable批註的方法。

那樣你不必慢你的測試用@DirtiesContext標記你所有的測試。

緩存相關的Spring配置放在他們自己的Spring配置文件中,例如。 applicationContext-cache.xml文件。

僅在運行應用程序li​​ve時包含該applicationContext-cache.xml文件,但不包含在您的測試中。

如果你特別想測試緩存,那麼你需要@DirtiesContext註解。

+0

我有@Cacheable註釋的方法,因此CacheManager必須始終存在。 – 2014-06-26 05:00:37

+0

我正在運行沒有CacheManager的測試,並且在運行測試時緩存註釋被忽略。如果我想測試緩存功能,我會在測試設置中包含applicationContext-cache.xml,並用@DirtiesContext標記它。 – 2014-06-30 11:50:19

1

我不知道問題是否仍然存在,但這裏有一個簡單/合適的解決方案(不需要在所有測試中添加@DirtiesContext)。避免@DirtiesContext允許您爲所有集成測試只有一個共享上下文(例如,通過maven運行,或者在IDE中運行所有測試)。這避免了在同一時間開始的多個上下文導致的多個問題。

<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}"/> 

在你的測試(集成測試),設置這些屬性

ehcacheManager.acceptExisting=true 
ehcacheManager.shared=false 

它允許Spring爲每個測試創建一個EhcacheManager(ehcache的),但如果 EhcacheManager具有相同名稱的存在, Spring將重用它。而且Spring也不會在@DirtiesContext註釋的上下文中銷燬/關閉它。

這個想法很簡單,你可以在使用@DirtiesContext時阻止EhcacheManager的銷燬。

這適用於使用Spring 4和EhCache:2.5+的情況。在Spring 3中,您必須擴展EhCacheManagerFactoryBean以添加這兩個屬性。

不要忘記在每次測試之前清除緩存:)

相關問題