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.
