Java-在IntegrationTests中禁用带有Redis的Spring会话
作者:互联网
我正在使用带有redis的spring会话,但是我想在进行测试时禁用它.我的课有注释:
@ActiveProfiles("integrationtests")
我的application-integrationtests.tml文件包含:
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
但仍然失败:
Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
如果我打开redis-server,则测试工作正常,但是我当然不想一直保持这种状态; )
//更新
香港专业教育学院一直在尝试
@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integrationtests")
和具有排除的Redis的Application.class:
@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
但是它失败了:
Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]
spring autoconfigure debug看到Ive排除了此类,但没有效果:
Exclusions:
-----------
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
解决方法:
我在测试时用于解决此Redis依赖性的解决方法是在测试时使用@ConditionalOnProperty批注并设置属性(testing = true).
使用以下代码进行会话配置:
@Configuration
public class SessionConfig {
@ConditionalOnProperty(name = "testing", havingValue = "false", matchIfMissing = true)
@EnableRedisHttpSession
public static class RedisSessionConfig {
}
@ConditionalOnProperty(name = "testing", havingValue = "true")
@EnableSpringHttpSession
public static class MapSessionConfig {
@Bean
public SessionRepository<ExpiringSession> sessionRepository() {
return new MapSessionRepository();
}
}
}
以及以下用于单元测试的代码:
@RunWith(SpringRunner.class)
@TestPropertySource(properties = "testing=true")
@SpringBootTest(classes = Application.class)
public class MyTest {
}
标签:spring-data-redis,redis,spring-session,spring,java 来源: https://codeday.me/bug/20191111/2022444.html