如何使用mockito测试ehcache?

时间:2013-04-12 14:42:18

标签: java mockito ehcache springmockito

Im helper方法使用ehcache,以减少对Db的查询。现在想要实现JUnit + Mockito测试以确保ehcache正常工作。有这样的测试变体:

@Autowired
private DBService service;
@Autowired
private DiscountHelper discountHelper;

@Autowired
private CacheManager cacheManager;

@Before
public void setUp() throws Exception {
    assertNotNull(cacheManager);
}

@Test
public void testGetDiscountWithCache() throws RuntimeException,
        InterruptedException {

    String id1 = "id1";
    String id2 = "id2";
    String id3 = "id3";

    List<String> discountsId = new ArrayList<String>();
    discountsId.add(id1);
    discountsId.add(id2);
    discountsId.add(id3);

    List<Map<String, Object>> attrList = new ArrayList<Map<String, Object>>();
    attrList.add(new Discount().getAttributes());
    attrList.add(new Discount().getAttributes());
    attrList.add(new Discount().getAttributes());

    Cache cache = cacheManager.getCache(DiscountHelper.CACHE_NAME);

    assertNotNull(cache);
    assertEquals(0, cache.getSize());

    // First run with empty cache
    when(service.getAllItems(eq(Discount.TABLE_NAME))).thenReturn(attrList);
    List<Discount> actualResult = discountHelper
            .getAllDiscountsUsingCache();
    assertNotNull(actualResult);
    assertEquals(attrList.size(), actualResult.size());
    verify(service).getAllItems(eq(Discount.TABLE_NAME));

    cache = cacheManager.getCache(DiscountHelper.CACHE_NAME);
    // In cache should be 1 record
    assertNotNull(cache);
    assertEquals(1, cache.getSize());

}

测试方法是:

@Cacheable(cacheName = CACHE_NAME, refreshInterval = 1000 * 900, decoratedCacheType = DecoratedCacheType.REFRESHING_SELF_POPULATING_CACHE)
public List<Discount> getAllDiscountsUsingCache() throws RuntimeException,
        InterruptedException {
    List<Map<String, Object>> result = dbService
            .getAllItems(Discount.TABLE_NAME);
    List<Discount> discountList = new ArrayList<Discount>();
    for (Map<String, Object> entry : result) {
        discountList.add(new Discount(entry));
    }
    return discountList;
}

这完美有效。在测试中,我确信在调用方法之后,我在缓存中得到了一些东西。如您所见,我还验证了在db服务中调用了getAllItems方法。这对于第一次调用很有用。接下来,我在同一个测试中添加第二次调用discountHelper.getAllDiscountsUsingCache():

when(service.getAllItems(eq(Discount.TABLE_NAME))).thenReturn(attrList);
actualResult = discountHelper
            .getAllDiscountsUsingCache();
assertNotNull(actualResult);
assertEquals(attrList.size(), actualResult.size());
verify(service, times(0)).getAllItems(eq(Discount.TABLE_NAME));

所以我只想检查第二次调用时,通过此验证方法getAllItems不会调用DB服务:verify(service,times(0))。getAllItems(eq(Discount.TABLE_NAME));结果将从缓存中获得。

但是它不起作用,我仍然可以调用DB方法。我找到了这个教程http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/并尝试使用委托对象进行ehcache测试,但它仍然会在测试时调用db方法。怎么了? 顺便说一下,在生产中,ehcache在tomcat的日志中看起来很好,所以这是一个测试问题。任何建议如何解决?

1 个答案:

答案 0 :(得分:2)

在第二次调用中,您在Spring上下文初始化期间使用Springockito创建的相同模拟对象(服务)(我假设从问题的标签中)。模拟会记住第一次调用时的getAllItems()调用。你可以:

  • 使用reset(service)
  • 重置第二次调用之前的模拟

    如果仍有一个(不是两个)getAllItems()调用,则
  • 第二次调用后检查)。