我已经在一个spring引导应用程序中安装了spring。1实现了缓存驱逐的api。我要为此写一封信。
下面是cacheManager在我的Configuration.java中的配置代码
@Bean
public CacheManager cacheManager()
{
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return new EhCacheCacheManager(factoryBean.getObject());
}在我的测试类中,我无法模拟CacheManager,因为无法读取ehcache.xml。
这是我的testClass:
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
@SpringBootTest
@RunWith(SpringRunner.class)
public class DriverServiceTest
{
@InjectMocks
private DriverServiceImpl driverServiceImpl = null;
@Mock
private DriverMongoRepository mongoRepository;
@Autowired
private CacheManager ehCacheManager;
@TestConfiguration
public static class TestConfig
{
@Bean
public CacheManager cacheManager()
{
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return new EhCacheCacheManager(factoryBean.getObject());
}
}
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
}
@Test
public void testEvictCache() throws Exception
{
//Trying to mock ehCacheManager similar to cacheManager() of configuration.java
String response = driverServiceImpl.evictCache("dummyUser", "getDriver", "1");
}
}由于我的ehCacheManager没有被正确地模拟,所以在缓存缓存=ehCacheManager.getCache(“getDriver”)处得到空指针;
有人能帮我模拟一下CacheManager,类似于configuration.java的cacheManager()吗
希望我的问题是清楚的!提前谢谢
发布于 2020-03-05 10:15:04
下面是我如何实现spring框架提供的ehcache缓存的示例代码。
@SpringBootTest
public class DriverServiceTest
{
@InjectMocks
private DriverServiceImpl driverServiceImpl = null;
@Mock
private CacheManager mockCacheManager;
private static CacheManager cacheManager;
@BeforeClass
public static void initialClassSetUp()
{
@SuppressWarnings("resource")
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
cacheManager = context.getBean("cacheManager", org.springframework.cache.ehcache.EhCacheCacheManager.class);
}
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
}
@Configuration
@EnableCaching
static class SpringConfig
{
@Bean
public CacheManager cacheManager()
{
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setCacheManager(factoryBean.getObject());
return ehCacheCacheManager;
}
}
@Test
public void testEvictCacheKeyNotExist() throws Exception
{
Cache cache = cacheManager.getCache("getSubaccounts");
ReflectionTestUtils.setField(driverServiceImpl , "ehCacheManager", cacheManager);
when(mockCacheManager.getCache(Mockito.anyString()))
.thenReturn(cache);
String actualValue = driverServiceImpl.evictCache("dummyUserName", "getSubaccounts",
"dummyKeyName");
assertTrue(actualValue.equals("key not exists"));
}以下是要注意的几点:
ReflectionTestUtils.setField(driverServiceImpl,"ehCacheManager",cacheManager);
还有塔达!现在你可以断言你的案子了!!
https://stackoverflow.com/questions/59646235
复制相似问题