如何检查是否在集成测试中调用了@KafkaListener? [英] How to check if @KafkaListener was invoked in integration test?

查看:26
本文介绍了如何检查是否在集成测试中调用了@KafkaListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为整个 kafka 流程编写一个集成测试.

I would like to write an integration test for whole kafka flow.

在我的生产代码中,我有:

In my production code I have:

@KafkaListener(topics = "myTopic")
public void listen(@Payload String payload) {
    log.debug("processing payload: '{}' ", payload);
    // business logic here
}

在我的测试代码中,我使用 KafkaProducerproducer; 向特定主题发送消息.

In my test code I use KafkaProducer<String, String> producer; to send messages to specific topic.

我想要一个钩子来表明 @KafkaListener 被调用了.我可以在测试中插入一些延迟,但这是一种不好的做法,我想避免它.

I would like to have a hook that would indicate that @KafkaListener was called. I could insert some delay into test but it's a bad practice and I want to avoid it.

有没有更好的方法来等待 @KafkaListener 被处理?

Is there any better way to wait for @KafkaListener being processed?

推荐答案

如果您的侦听器调用了某个服务,您可以为该服务注入一个模拟并验证它是否被调用.

If your listener invokes some service, you can inject a mock for that service and verify it was called.

此外,您可以将侦听器包装在测试用例中并添加倒计时锁存器.

Also, you can wrap your listener in the test case and add a count down latch.

请参阅此答案的示例.

这篇关于如何检查是否在集成测试中调用了@KafkaListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆