unit-testing - 在 Spring Boot Camel 中模拟 RedisTemplate

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

我正在尝试模拟 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'

最佳答案

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

&redisTemplate=#customTemplate

关于unit-testing - 在 Spring Boot Camel 中模拟 RedisTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48194385/

相关文章:

c# - 无法连接到Redis服务器

c# - 是否可以在 MS 测试中创建和关闭 wcf 服务主机

javascript - 设置或分配 'window.location' 时的 Jest 错误

java - 将实体映射到 entityDTO 的 Spring 引导问题

javascript - 简单节点/Express 应用程序无法识别 session 存储

redis key-value对key有什么限制?

c - 在 C 中将字符串作为文件句柄访问

python - S101 为 python 测试检测到断言的使用

spring-boot - 在 spring cloud gateway 中将配置从 Ribbon 更改为 Spring Cloud Load Balancer

java - Spring @Transactional 在 ApplicationServer 中不起作用