gpt4 book ai didi

java - 如何在 Camel 路由上模拟 Kafka Consumer 端点?

转载 作者:行者123 更新时间:2023-12-01 17:38:06 24 4
gpt4 key购买 nike

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

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

最佳答案

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

//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组件已经unit tested ,在代码库中复制所有这些测试是没有意义的。但是,如果您确实想针对真实的 Kafka 实例进行测试,您可以使用 test containersHere是一个完整的示例,来自 Camel 存储库本身,使用测试容器。

关于java - 如何在 Camel 路由上模拟 Kafka Consumer 端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61009937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com