2010-12-08 60 views
1

我在我的項目中使用Java,Hibernate和Spring。我正在處理大量數據,所以我已經實現了ehcache來首次緩存表和結果集。ehcache測試

但我不知道數據是否從緩存中加載下列時間。我如何測試這個?任何幫助將不勝感激。這是我配置ehcache的方式。

<prop key="hibernate.cache.use_second_level_cache">true</prop> 
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop> 
<prop key="hibernate.cache.use_query_cache">true</prop> 

然後我在類路徑中保存了ehcache.xml。這夠了嗎?我如何測試它是否工作?

回答

4

通過檢查日誌。將服務器的日誌記錄轉換爲DEBUG級別,您會看到緩存活動記錄在那裏。

+1

自動測試呢? – chrismarx 2012-08-02 14:44:14

0

您可以在您的測試資源(或測試的類路徑中)中添加ehcache.xml,以便它實例化ehcache。然後每當你測試使用這個緩存對象的方法時,它應該從緩存對象中搜索它。

或者您可以手動訪問您的測試代碼中的緩存。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath*:cache.xml"}) 
public abstract class TestEcacheSpring { 

    @Autowired 
    EcacheSpringtest test; 

    @Test 
    public void test(){ 
    test.getName("test"); 
    test.getName("test"); 
    } 

}