[英]KStream with Testbinder - Spring Cloud Stream Kafka
I recently started looking into Spring Cloud Stream for Kafka, and have struggled to make the TestBinder work with Kstreams. 我最近开始研究Kafka的Spring Cloud Stream,并努力使TestBinder与Kstreams一起使用。 Is this a known limitation, or have I just overlooked something?
这是一个已知的限制,还是我只是忽略了一些东西?
This works fine: 这工作正常:
String processor: 字符串处理器:
@StreamListener(TopicBinding.INPUT)
@SendTo(TopicBinding.OUTPUT)
public String process(String message) {
return message + " world";
}
String test: 字符串测试:
@Test
@SuppressWarnings("unchecked")
public void testString() {
Message<String> message = new GenericMessage<>("Hello");
topicBinding.input().send(message);
Message<String> received = (Message<String>) messageCollector.forChannel(topicBinding.output()).poll();
assertThat(received.getPayload(), equalTo("Hello world"));
}
But when I try to use KStream in my process, I can't get the TestBinder to be working. 但是,当我尝试在流程中使用KStream时,无法使TestBinder正常工作。
Kstream processor: Kstream处理器:
@SendTo(TopicBinding.OUTPUT)
public KStream<String, String> process(
@Input(TopicBinding.INPUT) KStream<String, String> events) {
return events.mapValues((value) -> value + " world");
}
KStream test: KStream测试:
@Test
@SuppressWarnings("unchecked")
public void testKstream() {
Message<String> message = MessageBuilder
.withPayload("Hello")
.setHeader(KafkaHeaders.TOPIC, "event.sirism.dev".getBytes())
.setHeader(KafkaHeaders.MESSAGE_KEY, "Test".getBytes())
.build();
topicBinding.input().send(message);
Message<String> received = (Message<String>)
messageCollector.forChannel(topicBinding.output()).poll();
assertThat(received.getPayload(), equalTo("Hello world"));
}
As you might have noticed, I omitted the @StreamListener from the Kstream processor, but without it it doesn't seem like the testbinder can find the handler. 您可能已经注意到,我从Kstream处理器中省略了@StreamListener,但是如果没有它,似乎testbinder无法找到该处理程序。 (but with it, it doesn't work when starting up the application)
(但是使用它,在启动应用程序时不起作用)
Is this a known bug / limitation, or am I just doing something stupid here? 这是已知的错误/限制,还是我只是在这里做一些愚蠢的事情?
The test binder is only for MessageChannel-based binders (subclasses of AbstractMessageChannelBinder
). 测试联编程序仅适用于基于MessageChannel的联编程序(
AbstractMessageChannelBinder
子类)。 The KStreamBinder
does not use MessageChannel
s. KStreamBinder
不使用MessageChannel
。
You can testing using the real binder and an embedded kafka broker, provided by the spring-kafka-test module. 您可以使用spring-kafka-test模块提供的真实资料夹和嵌入式kafka代理进行测试。
Also see this issue . 另请参阅此问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.