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
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 {
}
Create a "test" profile specific yml (application-test.yml)
spring.session.store-type=none
and the test class should have the annotation
@ActiveProfiles("test")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With