MockConsumer

MockConsumer is a concrete Consumer to be used for unit testing.

Note
MockConsumer is used in Kafka Streams' TopologyTestDriver to write tests to verify the behavior of topologies.

MockConsumer takes a single OffsetResetStrategy to be created.

import org.apache.kafka.clients.consumer._
val consumer = new MockConsumer[String, String](OffsetResetStrategy.EARLIEST)

While being created, MockConsumer initializes the internal registries and counters.

Beside Consumer API, MockConsumer defines its own testing-specific API.

Table 1. MockConsumer API
Method Description

addEndOffsets

void addEndOffsets(Map<TopicPartition, Long> newOffsets)

addRecord

void addRecord(ConsumerRecord<K, V> record)

closed

boolean closed()

rebalance

void rebalance(Collection<TopicPartition> newAssignment)

scheduleNopPollTask

void scheduleNopPollTask()

schedulePollTask

void schedulePollTask(Runnable task)

setException

void setException(KafkaException exception)

updateBeginningOffsets

void updateBeginningOffsets(
  Map<TopicPartition, Long> newOffsets)

updateEndOffsets

void updateEndOffsets(
  Map<TopicPartition, Long> newOffsets)

updatePartitions

void updatePartitions(
  String topic,
  List<PartitionInfo> partitions)
Table 2. MockConsumer’s Internal Properties (e.g. Registries, Counters and Flags)
Name Description

subscriptions

Used when…​FIXME

partitions

Map<String, List<PartitionInfo>> partitions

Used when…​FIXME

records

Map<TopicPartition, List<ConsumerRecord<K, V>>> records

Used when…​FIXME

paused

Kafka’s TopicPartition paused

Used when…​FIXME

closed

Flag that says whether…​FIXME Defaults to false

Used when…​FIXME

beginningOffsets

Map<TopicPartition, Long> beginningOffsets

Used when…​FIXME

endOffsets

Map<TopicPartition, List<Long>> endOffsets

Used when…​FIXME

pollTasks

Queue<Runnable>

Used when…​FIXME

exception

A KafkaException if thrown

Used when…​FIXME

wakeup

Flag that says whether…​FIXME (a Java AtomicBoolean)

Used when…​FIXME

committed

Map<TopicPartition, OffsetAndMetadata> committed

Used when…​FIXME

results matching ""

    No results matching ""