gpt4 book ai didi

java - 单元测试 MessageListener 类

转载 作者:行者123 更新时间:2023-12-02 01:24:15 28 4
gpt4 key购买 nike

如何对实现 spring-kafka MessageListener 接口(interface)的类进行单元测试?我有一个监听器类,我正在使用 onMessage 函数手动监听主题。这个函数很简单,就是接收消息。

我的设置是使用 Spring 5.8、Spring-Kafka 2.2.7、Spring-Kafka-Test、JUnit 和 WITHOUT spring boot。

我一直在尝试 Spring 引用文档和其他帖子中的一堆不同示例,但似乎没有一个示例显示测试实现 MessageListener 的 Listener 类的简单方法。

我不确定是否需要设置 EmbeddedKafkaBroker 或 EmbeddedKafkaRule 或者是否有不同的测试方法。当我尝试使用 EmbeddedKafkaRule 时,出现 NoClassDefFound 错误。

但是我不明白这个测试用例如何影响我的 onMessage 函数。

@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
public class listenerTest {

private String topic = "someTopic";

@ClassRule
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, topic);

private CountDownLatch countDownLatch;

@Before
public void setUpTests (){
Map<String, Object> sProps = KafkaTestUtils.senderProps(embeddedKafka.getEmbeddedKafka().getBrokersAsString());

ProducerFactory producer = new DefaultKafkaProducerFactory<String, String> (sProps);

kafkaTemplate = new KafkaTemplate<>(producer);

kafkaTemplate.setDefaultTopic(topic);

countDownLatch = new CountDownLatch (1);

}

@Test
public void testReceiver(){
kafkaTemplate.sendDefault("message");
assertEquals(latch.getCount(), 0);
}

我想要单元测试的类

public class listener implements BatchAcknowledgingMessageListener<String, String>{

private CallbackInterface callback;

public listener(CallbackInterface callback){
this.callbackI = callback;
}

@Override
public void onMessage(List<ConsumerRecord<String, String>> records, Acknowledgment ack){
this.callbackI.handleMessage();
ack.acknowledge();
}
}

这会引发一个奇怪的错误,表明...NoClassDefFound

最佳答案

对于纯单元测试,您不需要嵌入式代理,您应该直接调用监听器。

注入(inject)模拟回调并验证它是否被正确调用。

When I tried to directly call the onMessage function to test it I get an error that says Container should not be calling function onMessage.

您调用了错误的onMessage...

public interface BatchMessageListener extends MessageListener {

@Override
default void onMessage(Message message) {
throw new UnsupportedOperationException("Should never be called by the container");
}

@Override
void onMessageBatch(List<Message> messages);

}

编辑

public class MyListener implements BatchAcknowledgingMessageListener<String, String> {

private final MyService service;

public MyListener(MyService service) {
this.service = service;
}

@Override
public void onMessage(List<ConsumerRecord<String, String>> data, Acknowledgment acknowledgment) {
data.forEach(dat -> this.service.call(dat.value()));
acknowledgment.acknowledge();
}

public interface MyService {

void call(String toCall);

}

}

class So57192362ApplicationTests {

@Test
void test() {
MyService service = mock(MyService.class);
MyListener listener = new MyListener(service);
Acknowledgment acknowledgment = mock(Acknowledgment.class);
listener.onMessage(Collections.singletonList(new ConsumerRecord<>("foo", 0, 0L, null, "bar")), acknowledgment);
verify(service).call("bar");
verify(acknowledgment).acknowledge();
verifyNoMoreInteractions(service, acknowledgment);
}

}

关于java - 单元测试 MessageListener 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57192362/

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