5

我的辅助方法使用 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 方法在测试。怎么了?顺便说一句,正如我在 tomcat 的日志中看到的那样,在生产环境中 ehcache 工作得很好,所以这是一个测试问题。任何建议如何解决它?

4

1 回答 1

2

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

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

或者

  • 在第二次调用之后检查)是否还有一个(不是两个)getAllItems() 调用。
于 2013-04-13T23:13:52.023 回答