У меня есть базовое приложение 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);
}
}