kafka
kafka copied to clipboard
MINOR: Add MockProducer and MockConsumer testing examples
Motivation
This pull request addresses a common pain point for new users learning how to develop applications with Kafka clients: testing without relying on a full Kafka cluster.
The existing examples primarily focus on runtime operation. These new examples show the proper use of the built-in MockProducer and MockConsumer classes for writing fast, reliable, and decoupled unit tests.
Changes
Adds a new directory for testing examples: examples/src/main/java/kafka/examples/testing/
-
MockProducerExample.java: Demonstrates basic usage, topic routing, metadata checks, and error simulation. -
MockConsumerExample.java: Demonstrates basic usage, message processing, offset management (seek/commit), and partition rebalancing. -
README.md: Provides instructions on how to compile and run the new examples using Gradle, along with guidance on when to use mocks versus real brokers.
Notes
The example files follow the style of existing examples in the examples module and include the Apache License header.