无法连接到 Redis 运行 Junit 测试

Unable to connect to Redis running a Junit Test

我有一个基本的 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。 您可以查看有关此情况的有用博客 post mentinoned here。 或者 关于 Whosebug 的问题。