Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rcardin/kafka-testing
Kafka Unit Testing Usine Spring Kafka and MockConsumer/MockProducer Classes
https://github.com/rcardin/kafka-testing
kafka spring-kafka unit-testing
Last synced: 16 days ago
JSON representation
Kafka Unit Testing Usine Spring Kafka and MockConsumer/MockProducer Classes
- Host: GitHub
- URL: https://github.com/rcardin/kafka-testing
- Owner: rcardin
- Created: 2022-10-04T20:03:29.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-06T19:49:38.000Z (over 2 years ago)
- Last Synced: 2024-11-17T12:28:40.893Z (3 months ago)
- Topics: kafka, spring-kafka, unit-testing
- Language: Java
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kafka Unit Testing using Spring Kafka and `MockProducer`/`MockConsumer` Classes
Inside the Kafka library, there are two mock implementation of the `Consumer` and
`Producer` interfaces. These classes are `MockConsumer` and `MockProducer`. These classes are
useful for unit testing Kafka consumers and producers. Please, refer to the official documentation
to understand how to use them in the wild:* [How to build your first Apache KafkaProducer application](https://developer.confluent.io/tutorials/creating-first-apache-kafka-producer-application/confluent.html)
* [How to build your first Apache KafkaConsumer application](https://developer.confluent.io/tutorials/creating-first-apache-kafka-consumer-application/confluent.html)The [Spring Kafka](https://docs.spring.io/spring-kafka/docs/current/reference/html/) project is a
great library to build client application using abstraction of the
Kafka consumers and producers managed by Spring. It abstracts the producer using a `KafkaTemplate` type
, and the annotation `@KafkaListener` to consume messages from a topic.Can we use the `MockConsumer` and `MockProducer` classes with Spring Kafka? The answer is yes, and
this is what this project is about.Please, refer to the `src/test` folder to see how.
Is the use of the `MockConsumer` and `MockProducer` classes with Spring Kafka a good idea? Well, I
don't think so. In the case of the consumer, we can call the method annotated with `@KafkaListener`
directly. Otherwise, in the case of the producer, we can use the `KafkaTemplate` type to
create a mock using Mockito, or some other mocking framework.🤷♂️