问题
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