【问题标题】:Mock RedisTemplate in Spring Boot Camel在 Spring Boot Camel 中模拟 RedisTemplate
【发布时间】:2018-01-10 19:01:30
【问题描述】:

我正在尝试模拟 RedisTemplate 进行单元测试。我知道可以使用 Camel Test API 模拟 Redis,但是我有很多 Redis 请求,我发现 Mockito API 更简单易用。 下面是我写的测试,它抛出了JedisConnectionException

那么为什么我的模拟不起作用?

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(CustomerRoute.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@EnableAutoConfiguration(exclude = RedisAutoConfiguration.class)
public class CustomerRouteTest extends CamelTestSupport {

@Autowired
private CamelContext camelContext;

@Override
protected CamelContext createCamelContext() throws Exception {
    return camelContext;
}

@Test
public void routeIsProcessingTheFile() throws Exception {
   // ....
}

@Configuration
static class Config {

    @Bean
    RedisTemplate<?, ?> redisTemplate() {
        RedisTemplate<?, ?> template = mock(RedisTemplate.class);
        RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class);
        RedisConnection connection = mock(RedisConnection.class);

        when(template.getConnectionFactory()).thenReturn(connectionFactory);
        when(connectionFactory.getConnection()).thenReturn(connection);

        when(template.opsForSet()).thenReturn(mock(SetOperations.class));
        when(template.opsForHash()).thenReturn(mock(HashOperations.class));

        return template;
    }
}
}

我正在使用:

'org.apache.camel:camel-spring-boot-starter:2.20.1'
'org.apache.camel:camel-spring-redis:2.20.1'

【问题讨论】:

    标签: unit-testing spring-boot redis apache-camel mockito


    【解决方案1】:

    问题是我没有在toD URI 中包含模板,而骆驼使用了一些默认模板(我猜)。所以解决方法是:

    &redisTemplate=#customTemplate
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2017-07-30
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2020-03-28
      • 2018-01-17
      • 2019-11-29
      • 1970-01-01
      相关资源
      最近更新 更多