0

I am building an app with spring-boot. In order to avoid sticky-session related problems, I put in place a redis session store by adding those lines in pom.xml :

<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>

and those lines in application.properties :

spring.redis.host=localhost
spring.redis.password=secret
spring.redis.port=6379

It worked like a charm. I was surprised that it worked even if I did not use the annotation @EnableRedisHttpSession. At the beginning, I found it nice.

Problem : I have a Spring configuration for the real application and also a Spring configuration dedicated to unit tests. The Redis connection is useless in the unit test environment and make the test fail if I do not install a Redis server in the test environment.

I could eventually install a Mock Redis as a maven dependency but it would be cleaner if I found a way to disable this useless connection.

Any idea?

Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • 1
    Spring Boot's `SessionAutoConfiguration` kicks in. You can exclude it in your application (`@SpringBootApplication` or properties), see [here](https://github.com/pivotalsoftware/pivotal-cla/blob/b35645d709fc4bae31b353eaddbd15374cf2570f/src/main/java/io/pivotal/cla/GithubClaApplication.java) – mp911de Jun 01 '16 at 08:45
  • @mp911de Adding this works only if I apply it on both application conf and test conf. If I manually add `@EnableRedisHttpSession` only on application conf, it is also detected by the test configuration. Do you have an idea about how I can prevent the test conf to detect the `@EnableRedisHttpSession` on the app conf? – Arnaud Denoyelle Jun 01 '16 at 09:41
  • There's no recovery from an `@Enable...` annotation. Enabled is enabled. I think profiles could help you here. – mp911de Jun 01 '16 at 09:52
  • @mp911de I found the solution to the second problem. I excluded the app configuration from the component-scan of the test configuration and it worked. Your solution worked for the problem exposed in the question so it you wish to post an answer, I will upvote and accept it. – Arnaud Denoyelle Jun 01 '16 at 10:06

2 Answers2

2

To solve this Redis unit test issue I'm using @ConditionalOnProperty and setting a property while running the unit test (testing=true). So, I'm using the following code for session configuration:

@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();
        }
    }
}

And the following code for the unit test:

@RunWith(SpringRunner.class)
@TestPropertySource(properties = "testing=true")
@SpringBootTest(classes = Application.class)
public class MyTest {
}
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
0

You can do this with @Profile annotation. You can make your redis configuration only works in NOT unit test profile. like this:

@Configuration
@Profile("!" + Constants.SPRING_PROFILE_UNITTEST)
public class RedisConfiguration {
    @Bean
    public RedisTemplate getRedisTemplate() {...}
}
Mavlarn
  • 3,807
  • 2
  • 37
  • 57