Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable spring session with redis in integrationtests

I am using spring session with redis, but I want to disable it while doing tests. My class is annotated:

@ActiveProfiles("integrationtests")

and my application-integrationtests.tml file contains:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

but it still fails with:

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

tests work if I turn redis-server on, but ofcourse I dont want to keep it that way ; )

//update

ive been trying with

@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integrationtests")

and Application.class with excluded Redis:

@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}
}

but it fails with:

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 see that Ive excluded this class, but it has no effect:

Exclusions:
-----------

org.springframework.boot.autoconfigure.session.SessionAutoConfiguration

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration
like image 307
hi_my_name_is Avatar asked Jan 24 '17 11:01

hi_my_name_is


2 Answers

The workaround I use to solve this Redis dependency while testing is to use the @ConditionalOnProperty annotation and setting a property (testing=true) while testing.

Use 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 {
}
like image 116
Italo Borssatto Avatar answered Nov 20 '22 01:11

Italo Borssatto


Create a "test" profile specific yml (application-test.yml)

spring.session.store-type=none

and the test class should have the annotation

@ActiveProfiles("test")
like image 2
DJones Avatar answered Nov 20 '22 00:11

DJones