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

时间:2018-08-25 17:00:28

标签: 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);           
    }

}

1 个答案:

答案 0 :(得分:0)

在质疑的测试中,使用的是与Redis相关的服务。 因此,您需要模拟redis。 您可以查看有关here的有用博客文章,以了解这种情况。 或关于堆栈溢出的this问题。