[Spring Boot] Unit Testing Kafka Producer with Producer Listener
[Spring Boot] Unit Testing Kafka Producer with EmbeddedKafkaBroker
[Spring Boot] Unit Testing Kafka Producer with Producer Callback
This article covers how to write unit tests for Kafka Producers with Producer Listeners. It is similar to Producer Callback implementing
ListenableFutureCallback. With this approach, there are a couple of drawbacks in my opinion.
- Testing can be complicated due to
- Even though the functionality is related to Kafka Producer, this callback is handled by Spring Framework rather than spring-kafka.
Let’s look at another option: Producer Listener.
KafkaAutoConfiguration.java, there are a couple of hints.
First of all, it is possible to override
ProducerListener bean with our own. If there is no bean provided for
ProducerListener, it will set up
LoggingProducerListener by default.
KafkaTemplate bean will be created using our custom
ProducerListener bean. Of course, if you are customizing
KafkaTemplate bean, then you would need to add
kafkaTemplate.setProducerListener(kafkaProducerListener) in your bean initialization.
Producer Listener & Configuration
For simplicity, I will add some print statements. But these are the places where you can put some business logic to handle both success and error cases.
As seen earlier, a bean is required to override the default