[英]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.