【发布时间】:2016-06-01 08:35:41
【问题描述】:
我正在使用spring-boot 构建一个应用程序。为了避免与粘滞会话相关的问题,我通过在 pom.xml 中添加这些行来放置一个 redis 会话存储:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
以及 application.properties 中的那些行:
spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379
它就像一个魅力。即使我没有使用注释@EnableRedisHttpSession,它也能正常工作,我感到很惊讶。一开始觉得挺好看的。
问题:我有一个用于实际应用程序的 Spring 配置,还有一个专门用于单元测试的 Spring 配置。 Redis连接在单元测试环境中没有用,如果我在测试环境中没有安装Redis服务器,会导致测试失败。
我最终可以安装一个 Mock Redis 作为 maven 依赖项,但如果我找到一种方法来禁用这个无用的连接,它会更干净。
有什么想法吗?
【问题讨论】:
-
Spring Boot 的
SessionAutoConfiguration开始使用。您可以在应用程序中排除它(@SpringBootApplication或属性),请参阅 here -
@mp911de 仅当我将其应用于应用程序 conf 和测试 conf 时,才添加此功能。如果我仅在应用程序conf上手动添加
@EnableRedisHttpSession,它也会被测试配置检测到。您知道如何防止测试配置检测到应用配置上的@EnableRedisHttpSession吗? -
无法从
@Enable...注释中恢复。 Enabled 已启用。我认为个人资料在这里可以为您提供帮助。 -
@mp911de 我找到了第二个问题的解决方案。我从测试配置的组件扫描中排除了应用程序配置并且它有效。您的解决方案适用于问题中暴露的问题,因此您希望发布答案,我会投票并接受它。
标签: java unit-testing spring-boot junit spring-data-redis