【问题标题】:How to create unit test with kafka embedded in the spring cloud stream如何使用嵌入在 Spring Cloud Stream 中的 kafka 创建单元测试
【发布时间】:2017-09-05 22:23:02
【问题描述】:

抱歉这个问题太笼统了,但是有人有一些关于如何使用嵌入的 kafka 执行生产者和消费者测试的教程或指南。我已经尝试了几个,但是有几个版本的依赖项,没有一个真正有效=/

我正在使用spring cloud stream kafka。

【问题讨论】:

    标签: junit apache-kafka spring-cloud spring-kafka spring-cloud-stream


    【解决方案1】:

    我们通常建议在测试中使用Test Binder,但如果您想使用嵌入式 kafka 服务器,可以这样做...

    将此添加到您的 POM...

    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka-test</artifactId>
        <scope>test</scope>
    </dependency>
    

    测试应用...

    @SpringBootApplication
    @EnableBinding(Processor.class)
    public class So43330544Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So43330544Application.class, args);
        }
    
        @StreamListener(Processor.INPUT)
        @SendTo(Processor.OUTPUT)
        public byte[] handle(byte[] in){
            return new String(in).toUpperCase().getBytes();
        }
    
    }
    

    application.properties...

    spring.cloud.stream.bindings.output.destination=so0544out
    spring.cloud.stream.bindings.input.destination=so0544in
    spring.cloud.stream.bindings.output.producer.headerMode=raw
    spring.cloud.stream.bindings.input.consumer.headerMode=raw
    spring.cloud.stream.bindings.input.group=so0544
    

    测试用例...

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class So43330544ApplicationTests {
    
        @ClassRule
        public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1);
    
        @Autowired
        private KafkaTemplate<byte[], byte[]> template;
    
        @Autowired
        private KafkaProperties properties;
    
        @BeforeClass
        public static void setup() {
            System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getBrokersAsString());
        }
    
        @Test
        public void testSendReceive() {
            template.send("so0544in", "foo".getBytes());
            Map<String, Object> configs = properties.buildConsumerProperties();
            configs.put(ConsumerConfig.GROUP_ID_CONFIG, "test0544");
            configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
            ConsumerFactory<byte[], byte[]> cf = new DefaultKafkaConsumerFactory<>(configs);
            Consumer<byte[], byte[]> consumer = cf.createConsumer();
            consumer.subscribe(Collections.singleton("so0544out"));
            ConsumerRecords<byte[], byte[]> records = consumer.poll(10_000);
            consumer.commitSync();
            assertThat(records.count()).isEqualTo(1);
            assertThat(new String(records.iterator().next().value())).isEqualTo("FOO");
        }
    
    }
    

    【讨论】:

    • 很好,Gary Russell 终于成功了,哈哈。非常喜欢。
    • 现在我有使用spring上下文的测试,但不使用kafka,例如对象之间比较的简单测试,当我执行所有testes时,这个测试会抛出与kafka的连接被拒绝错误。我可以决定哪些测试将使用嵌入式 kafka,哪些不使用?
    • 如果您使用 EnableBinding 测试 Spring Boot 应用程序,则不会;活页夹需要连接。您必须将这些测试与引导环境隔离开来。这就是为什么最好使用测试粘合剂的原因之一。
    • 请注意,不再需要 zkNodes(自 2.0 起),因为我们不再需要连接到 zookeeper 来配置主题。
    • 我们是否可以使用 TestSupportBinder 为“spring-cloud-stream-binder-kafka-streams”编写 junit 测试,其中我们使用 KStream 而不是 MessageChannel 处理数据?
    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多