繁体   English   中英

如何使用mockito测试ehcache?

[英]How to test ehcache using mockito?

我帮助方法使用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的日志中看起来很好,所以这是一个测试问题。 任何建议如何解决?

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

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM