当前位置: 代码迷 >> java >> 如何使用Powermockito测试缓存
  详细解决方案

如何使用Powermockito测试缓存

热度:97   发布时间:2023-08-04 09:08:54.0

我想使用嘲笑来测试我的缓存:

@Before
public void init() {
    cacheManager = PowerMockito.mock(CacheManager.class);
    cache = PowerMockito.mock(Cache.class);
}

@Test
public void test_Cache() throws Exception {

    ReflectionTestUtils.setField(csvReaderService, "csvFile", csvFileOK);

    Cache cache = cacheManager.getCache(CACHE_NAME);

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

    csvReaderService.readCSV();

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

我在assertNotNull(cache);出错assertNotNull(cache); 这意味着缓存为空。 我应该在调用“ cacheManager.getCache(CACHE_NAME)”之前在某处初始化“ cacheManager”吗?

您必须设置模拟方法:

when(cacheManager.getCache(any(String.class))).thenReturn(cache );    
  相关解决方案