junit@SpringBootTest for org.springframework.cache.cache
我已经在SpringBoot应用程序中安装了SpringEhcache。为缓存逐出实现了1个api。我正在为此写junits 以下是my Configuration.java中cacheManager的配置代码junit@SpringBootTest for org.springframework.cache.cache,junit,ehcache,spring-boot-test,Junit,Ehcache,Spring Boot Test,我已经在SpringBoot应用程序中安装了SpringEhcache。为缓存逐出实现了1个api。我正在为此写junits 以下是my Configuration.java中cacheManager的配置代码 @Bean public CacheManager cacheManager() { EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
@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没有被正确地模拟,所以我在
Cache Cache=ehCacheManager.getCache(“getDriver”)
有人能帮我模拟一下类似configuration.java的CacheManager()的CacheManager吗
希望我的问题清楚!!
提前感谢,下面是我如何实现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)
还有塔达!!现在你可以坚持你的观点了 您可以发布测试用例的完整代码吗?为什么您不能读取ehcache.xml?您可以删除所有注释代码并写出您的测试用例和您想要断言的内容吗。你的测试代码毫无意义