如何使用Powermockito测试缓存

时间:2015-07-28 13:00:37

标签: java caching mockito

我想使用mockito测试我的缓存:

<link rel="stylesheet" href="css/main.scss">

我在@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()); } 中收到错误,这意味着缓存为空。 我应该在调用“cacheManager.getCache(CACHE_NAME)”之前在某处初始化“cacheManager”吗?

1 个答案:

答案 0 :(得分:0)

您必须设置模拟方法:

when(cacheManager.getCache(any(String.class))).thenReturn(cache );