How to check if @KafkaListener was invoked in integration test?

拟墨画扇 提交于 2020-01-25 08:32:06

问题


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
}

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

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.

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


回答1:


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.

See this answer for an example.



来源:https://stackoverflow.com/questions/58518844/how-to-check-if-kafkalistener-was-invoked-in-integration-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!