问题描述
我想使用嘲笑来测试我的缓存:
@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”吗?
1楼
您必须设置模拟方法:
when(cacheManager.getCache(any(String.class))).thenReturn(cache );