Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与redis的Spring会话-如何在集成测试中模拟它?_Spring_Spring Session - Fatal编程技术网

与redis的Spring会话-如何在集成测试中模拟它?

与redis的Spring会话-如何在集成测试中模拟它?,spring,spring-session,Spring,Spring Session,所以,我在我的项目中使用spring会话。 如何使用它在项目中编写集成测试?我应该为春季会议模拟一些东西吗?或者以任何合理的方式使用嵌入式redis 我看到过去有一些@EnableEmbeddedRedis注释,但似乎已被删除: //编辑 我试图将MockHttpSession传递给 mockMvc.perform(post("/register").session(mockHttpSession) 但是spring尝试连接redis,但还是失败了。好的,我刚刚在集成测试中使用配置文件禁用了r

所以,我在我的项目中使用spring会话。 如何使用它在项目中编写集成测试?我应该为春季会议模拟一些东西吗?或者以任何合理的方式使用嵌入式redis

我看到过去有一些@EnableEmbeddedRedis注释,但似乎已被删除:

//编辑

我试图将MockHttpSession传递给

mockMvc.perform(post("/register").session(mockHttpSession)

但是spring尝试连接redis,但还是失败了。

好的,我刚刚在集成测试中使用配置文件禁用了redis

@ActiveProfiles("integrationtests")
class RegisterControllerTest extends Specification {...
并将@EnableRedistpSession提取到其自己的类中:

@Configuration
@EnableRedisHttpSession
@Profile("!integrationtests")
public class RedisConfig {

}

它更像是解决方案,而不是解决方案,但我不需要在会话中测试任何内容。

您可以创建自己的连接工厂和重新序列化程序。然后SpringBoot将不会创建它们的默认bean

例如:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApplicationTest
{

    @Test
    public void contextLoads()
    {
    }


    @EnableRedisHttpSession
    @Configuration
    static class Config
    {
        @Bean
        @SuppressWarnings("unchecked")
        public RedisSerializer<Object> defaultRedisSerializer()
        {
            return Mockito.mock(RedisSerializer.class);
        }


        @Bean
        public RedisConnectionFactory connectionFactory()
        {
            RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class);
            RedisConnection connection = Mockito.mock(RedisConnection.class);
            Mockito.when(factory.getConnection()).thenReturn(connection);

            return factory;
        }
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(类=Application.class)
@WebAppConfiguration
公共类应用程序测试
{
@试验
public void contextLoads()
{
}
@启用RedistpSession
@配置
静态类配置
{
@豆子
@抑制警告(“未选中”)
公共重新序列化程序defaultRedisSerializer()
{
返回Mockito.mock(RedisSerializer.class);
}
@豆子
公共重新连接工厂连接工厂()
{
RedisConnectionFactory=Mockito.mock(RedisConnectionFactory.class);
RedisConnection=Mockito.mock(RedisConnection.class);
Mockito.when(factory.getConnection())。然后返回(connection);
返回工厂;
}
}
}
这项工作:

在HttpSessionConfig中,定义配置处于活动状态的配置文件:

@EnableRedisHttpSession
@Profile("prod")
class HttpSessionConfig {

}
在application-prod.properties中添加:

#REDIS SERVER
spring.redis.host=xxxxxxxx
然后在测试的application.properties中添加:

#REDIS SERVER
spring.data.redis.repositories.enabled=false
spring.session.store-type=none

我不建议使用mockHttpSession,因为它将在测试中绕过与spring会话库的集成。我会的

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ExampleControllerV2SpringSessionTest {

@Autowired
private WebApplicationContext wac;

@Autowired
private SessionRepository sessionRepository;

@Autowired
private SessionRepositoryFilter sessionRepositoryFilter;

//this is needed to test spring-session specific features
private MockMvc mockMvcWithSpringSession;

@Before
public void setup() throws URISyntaxException {

this.mockMvcWithSpringSession = MockMvcBuilders
   .webAppContextSetup(wac)
   .addFilter(sessionRepositoryFilter)
   .build();
}
}

知道在测试过程中很难始终准备好redis实例,我建议您在测试用例中使用此属性
spring.session.store type=hash\u map

请注意……如果使用spring引导,这将不起作用。Spring Boot使用自动配置设置Redis连接(当您的类路径上有Redis会话时,Spring Boot搜索Redis连接,如果找不到连接,则搜索失败)。它是Spring Boot,但版本低于1.4,甚至可能低于1.3,在1.4中,正如您所说的,在redis中自动配置获取。当问这个问题时,没有类似于
spring.session.store type=hash_map
的设置(它是
spring boot 1.3