[Spring Boot] Unit Testing Kafka Producer with Producer Listener

Jay Kim
2 min readJun 13, 2022

--

Previously,

[Spring Boot] Unit Testing Kafka Producer with EmbeddedKafkaBroker

[Spring Boot] Unit Testing Kafka Producer with Producer Callback

Introduction

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 ListenableFuture, ListenableFutureCallback.
  • 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.

Approach

Looking at 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.

KafkaAutoConfiguration.java

Secondly, 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.

KafkaAutoConfiguration.java

Implementation

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 ProducerListener.

Unit Test

--

--