首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >junit @SpringBootTest for org.springframework.cache.Cache

junit @SpringBootTest for org.springframework.cache.Cache
EN

Stack Overflow用户
提问于 2020-01-08 12:48:08
回答 1查看 2.5K关注 0票数 0

我已经在一个spring引导应用程序中安装了spring。1实现了缓存驱逐的api。我要为此写一封信。

下面是cacheManager在我的Configuration.java中的配置代码

代码语言:javascript
复制
 @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:

代码语言:javascript
复制
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()吗

希望我的问题是清楚的!提前谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-03-05 10:15:04

下面是我如何实现spring框架提供的ehcache缓存的示例代码。

代码语言:javascript
复制
 @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"));
    }

以下是要注意的几点:

  1. 需要在配置静态类上添加@EnableCaching注释和@Configuration。
  2. cacheManager值需要分配给initialClassSetUp()中的本地cacheManager,因为它应该在每个类中完成一次。

ReflectionTestUtils.setField(driverServiceImpl,"ehCacheManager",cacheManager);

还有塔达!现在你可以断言你的案子了!!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59646235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档