liveBook · Manning

MockConsumer

As explained in Kafka Streams in Action, Second Edition

MockConsumer is a Kafka client test class that simulates a consumer without reading records from a live Kafka broker.

MockConsumer simulates a Kafka consumer for unit tests, allowing an application to process records without connecting to a live broker. Inject it through the Consumer interface alongside a MockProducer, then verify both interactions and output. section 14.1.1

A scheduled-poll test controls an otherwise continuous consumer loop:

  • Schedule a final task that calls close, allowing runExchange to exit its loop without another test thread. section 14.1.1

Currency exchange unit test

Pass a MockConsumer and MockProducer to CurrencyExchangeClient, feed euro, pound, and yen records, and inspect mockProducer.history(). Map the produced records to values and compare them with the expected dollar conversions.

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains interface substitution, scheduled poll tasks, partition setup, record injection, shutdown, and output verification.
Examples
Uses a currency exchange loop with partition records, scheduled shutdown, and expected converted transactions.
What it assumes
Assumes Consumer and Producer interface usage plus a loop driven by poll(Duration), while supplying the test interactions explicitly.
What you can do after
Diagnose/debug consumer-producer behavior in repeatable unit tests without starting a Kafka broker.
How it connects
Connects Consumer, MockProducer, KafkaConsumer, unit testing, integration testing, and producer history assertions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.