繁体   English   中英

如何在骆驼路线上模拟 Kafka 消费者端点?

[英]How to Mock a Kafka Consumer endpoint on a Camel Route?

我有一个 Camel 端点,它基本上是一个 Kafka 消费者,它从一个主题中读取信息并将信息发送到数据库。 它工作正常,但是,我正在努力对其进行单元测试,因为我无法模拟 Kafka 端点。 谁能在骆驼路线中的卡夫卡消费者 mocking 中帮助我?

@Override
public void configure() {    
    from(kafka:eph?brokers=localhost:9092...).routeId("KafkaConsumer")
        .to(direct:updateDatabase)
}

要对您的路线进行单元测试,您可以使用标准的骆驼 spring 引导测试来完成。 在测试期间,Kafka 生产者(在 Camel 看来)可以与直接组件交换,并且可以在那里传递模拟消息。 要查看您的路由是否正确处理这些消息,可以使用Mock 端点

//Route definition
@Component
public class KafkaRoute extends RouteBuilder {

    public static final String KAFKA_ROUTE_NAME = "kafka-route";

    @Override
    public void configure() throws Exception {
        from("kafka:eph?brokers=localhost:9092").routeId(KAFKA_ROUTE_NAME)
                .log(LoggingLevel.INFO, "Message: ${body}  received on the topic: ${headers[kafka.TOPIC]} ")
                .to("direct:updateDatabase");

        from("direct:updateDatabase").log(LoggingLevel.INFO, "DB Updated.");

    }

}

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.component.kafka.KafkaConstants;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.CamelSpringBootRunner;
import org.apache.camel.test.spring.MockEndpoints;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("direct:*")
public class KafkaRouteTest {

    @Autowired
    CamelContext camelContext;

    @Produce
    ProducerTemplate mockKafkaProducer;

    @EndpointInject("mock:direct:updateDatabase")
    MockEndpoint finalSink;

    @Test
    public void testKafkaRoute() throws Exception {

        //Here we swap the FROM component in the KafkaRoute.KAFKA_ROUTE_NAME with a direct component, direct:kafka-from
        AdviceWithRouteBuilder.adviceWith(camelContext, KafkaRoute.KAFKA_ROUTE_NAME, routeBuilder -> {
            routeBuilder.replaceFromWith("direct:kafka-from");
        });

        Map<String, Object> headers = new HashMap<>();
        headers.put(KafkaConstants.TOPIC, "testTopic");

        //Send mock message to the route
        mockKafkaProducer.sendBodyAndHeaders("direct:kafka-from", "test-body", headers);


        //Assertions. You may do additional assertions with the likes of Mockito
        finalSink.expectedBodiesReceived("test-body");
        finalSink.expectedHeaderReceived(KafkaConstants.TOPIC, "testTopic");
        finalSink.assertIsSatisfied();

    }

}

Camel Kafka 组件已经过单元测试,在您的代码库中复制所有这些测试是没有意义的。 但是,如果您真的想针对真实的 Kafka 实例进行测试,您可以使用 测试容器 是一个完整的示例,来自 Camel 存储库本身,使用测试容器。

只需在属性中外部化端点 URI(例如,使用 Spring 属性工具)

from(consumerEndpoint).routeId("KafkaConsumer")

然后在您的生产配置中,您使用真正的端点

consumerEndpoint=kafka:eph?brokers=localhost:9092...

而在您的测试配置中,您使用的是直接端点

consumerEndpoint=direct:consumer

这很容易从骆驼路线测试中触发

producer.sendBody("direct:consumer", myMessageBody);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM