kafka icon indicating copy to clipboard operation
kafka copied to clipboard

MINOR: Add MockProducer and MockConsumer testing examples

Open zaracoder-stack opened this issue 1 month ago • 0 comments

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/

  1. MockProducerExample.java: Demonstrates basic usage, topic routing, metadata checks, and error simulation.
  2. MockConsumerExample.java: Demonstrates basic usage, message processing, offset management (seek/commit), and partition rebalancing.
  3. 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.

zaracoder-stack avatar Dec 07 '25 14:12 zaracoder-stack