spring - 无法连接到运行 Junit 测试的 Redis

标签 spring spring-mvc spring-boot redis junit4

我有一个基本的 SpringBoot 2.0.4.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并将其打包为可执行 JAR 文件,我根本不使用任何 Redis 配置

我创建了这个 Junit 测试:

@ContextConfiguration(classes={TestSystemConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AzureApplication.class) 
public class RoleServiceTests {

    @Autowired
    protected  RoleService  roleService;


    @Test
    public void testSaveAndFindByName() throws Exception {

        roleService.save(new Role(RolesEnum.ADMIN));
        assertNotNull (roleService.findByName(RolesEnum.ADMIN.getRoleName()));

    }
}

但是当我运行测试时我得到了这个异常:

org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:966)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:934)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:786)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:300)
    at org.springframework.data.redis.cache.DefaultRedisCacheWriter.execute(DefaultRedisCacheWriter.java:238)
    at org.springframework.data.redis.cache.DefaultRedisCacheWriter.get(DefaultRedisCacheWriter.java:109)
    at org.springframework.data.redis.cache.RedisCache.lookup(RedisCache.java:82)
    at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58)
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73)
    at org.springframework.cache.interceptor.CacheAspectSupport.findInCaches(CacheAspectSupport.java:525)
    at org.springframework.cache.interceptor.CacheAspectSupport.findCachedItem(CacheAspectSupport.java:490)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:372)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:316)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy131.getByName(Unknown Source)

但是像这样的其他 Junit 测试运行良好:

@ContextConfiguration(classes={TestSystemConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AzureCloudApplication.class) 
public class CompanyServiceTests {

    @Autowired
    protected  CompanyService  companyService;


    @Test
    public void testFindAll() throws Exception {

        Iterable<Company> companies = companyService.findAll();
        assertTrue (((Collection<?>) companies).size() > 0);            
    }


    @Test
    public void testCompanyUsers() throws Exception {

        Iterable<Company> companies = companyService.findAll();
        Company company = companies.iterator().next();

        assertNotNull (company);

        company = companyService.companyUsers(company.getId());
        assertTrue (((Collection<?>) company.getUsers()).size() > 0);           
    }

}

最佳答案

在被质疑的测试中使用了与redis相关的服务。 因此,您需要模拟 redis。 您可以查看有用的博客文章 mentinoned here关于这种情况。 或者 this关于计算器的问题。

关于spring - 无法连接到运行 Junit 测试的 Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019445/

相关文章:

java - Spring 3.2.4 中测试 Controller 的替代方案

Spring @Bean(name ="name") vs @Bean @Qualifier ("name")

jquery - ajax调用中返回ModelAndView的区别

spring-boot - 使用 MultipartFile 将图像上传到服务器会导致 HttpClientErrorException$BadRequest : 400 Bad Request:

Spring注入(inject)绑定(bind)到Instance

java - 如何使用 RequestMapping 将表单映射到 Controller 中的两种不同方法?

java - 如何在部署 war 之前或 tomcat 服务器重启后从数据库中清理表

java - Spring 4 : MappingJackson2HttpMessageConverter does not support application/javascript for jsonp

spring-boot - 多模块Vaadin项目生成文件到父模块中

spring-security - 登录成功后如何设置重定向?