我想使用mockito测试我的缓存:
@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);
中收到错误,这意味着缓存为空。
我应该在调用“cacheManager.getCache(CACHE_NAME)”之前在某处初始化“cacheManager”吗?
最佳答案
您必须设置模拟方法:
when(cacheManager.getCache(any(String.class))).thenReturn(cache );
关于java - 如何使用 Powermockito 测试缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677091/