在集成测试中禁用带有redis的spring会话

时间:2017-01-24 11:56:46

标签: java spring redis spring-session spring-data-redis

我正在使用带redis的spring会话,但是我想在进行测试时禁用它。我的课程注释:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
NameError: name 'SubElement' is not defined

我的application-integrationtests.tml文件包含:

@ActiveProfiles("integrationtests")

但它仍然失败:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
如果我打开redis-server,

测试工作,但当然我不想保持这种方式; )

//的更新

我一直在尝试

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

和Application.class排除了Redis:

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

但它失败了:

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

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

spring autoconfigure debug看到我已经排除了这个类,但它没有效果:

Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]

3 个答案:

答案 0 :(得分:3)

我在测试时用来解决此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 {
}

答案 1 :(得分:0)

没有直接的解决方案,但是有一个解决方法 ,比如下面的Test课程:

@SpringBootTest(classes = { ApplicationConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("integrationtests")
public class SpringBootRedisApplicationTest {

    @Autowired
    private StudentRepository studentRepository;

    @Test
    public void test() {
    }

}

您可以使用所有配置创建Base类,如:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.spring.boot.redis.repository" })
public class ApplicationConfig {

    @Bean
    public JedisConnectionFactory jedisConnFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPort(6379);
        return jedisConnectionFactory;
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnFactory());
        return redisTemplate;
    }

    @Bean
    public RedisHttpSessionConfiguration redisHttpSessionConfiguration() {
        return new RedisHttpSessionConfiguration();
    }
}

没有@EnableRedisHttpSession

的集成测试的子类
@Configuration
@Profile({ "integrationtests", "unittest" })
public class IntegrationTestConfig extends ApplicationConfig {

}

另一个@EnableRedisHttpSession用于所有其他配置文件,例如dev,staging和prod,类似于以下开发:

@Profile({ "dev" })
@Configuration
@EnableRedisHttpSession
public class DevConfig extends ApplicationConfig {

}

或者,您可以将dev设置为spring.profiles.active中指定application.properties的spring boot应用的默认配置文件,如下所示:

spring.profiles.active=dev

例如,您可以参考arpitaggarwal/spring-boot-redis

答案 2 :(得分:0)

创建特定于“测试”配置文件的yml(application-test.yml)

session.session.store-type=none

并且测试类应该具有注释

@ActiveProfiles("test")