Class MockConsumer<K,V>
java.lang.Object
org.apache.kafka.clients.consumer.MockConsumer<K,V>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Consumer<K,V>
public class MockConsumer<K,V> extends Object implements Consumer<K,V>
A mock of the
Consumer
interface you can use for testing code that uses Kafka. This class is not
threadsafe . However, you can use the schedulePollTask(Runnable)
method to write multithreaded tests
where a driver thread waits for poll(Duration)
to be called by a background thread and then can safely perform
operations during a callback.-
Constructor Summary
Constructors Constructor Description MockConsumer(OffsetResetStrategy offsetResetStrategy)
-
Method Summary
-
Constructor Details
-
MockConsumer
-
-
Method Details
-
assignment
- Specified by:
assignment
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.assignment()
-
rebalance
Simulate a rebalance event. -
subscription
- Specified by:
subscription
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscription()
-
subscribe
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection)
-
subscribe
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern, ConsumerRebalanceListener)
-
subscribe
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern)
-
subscribe
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection, ConsumerRebalanceListener)
-
assign
- Specified by:
assign
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.assign(Collection)
-
unsubscribe
public void unsubscribe()- Specified by:
unsubscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.unsubscribe()
-
poll
Deprecated.- Specified by:
poll
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.poll(long)
-
poll
- Specified by:
poll
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.poll(Duration)
-
addRecord
-
setException
Deprecated.UsesetPollException(KafkaException)
instead -
setPollException
-
setOffsetsException
-
commitAsync
public void commitAsync(Map<TopicPartition,OffsetAndMetadata> offsets, OffsetCommitCallback callback)- Specified by:
commitAsync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitAsync(Map, OffsetCommitCallback)
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Map)
-
commitAsync
public void commitAsync()- Specified by:
commitAsync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitAsync()
-
commitAsync
- Specified by:
commitAsync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitAsync(OffsetCommitCallback)
-
commitSync
public void commitSync()- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync()
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Duration)
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Map, Duration)
-
seek
- Specified by:
seek
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seek(TopicPartition, long)
-
seek
- Specified by:
seek
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seek(TopicPartition, OffsetAndMetadata)
-
committed
Deprecated.- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(TopicPartition)
-
committed
Deprecated.- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(TopicPartition, Duration)
-
committed
- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(Set)
-
committed
public Map<TopicPartition,OffsetAndMetadata> committed(Set<TopicPartition> partitions, Duration timeout)- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(Set, Duration)
-
position
- Specified by:
position
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.position(TopicPartition)
-
position
- Specified by:
position
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.position(TopicPartition, Duration)
-
seekToBeginning
- Specified by:
seekToBeginning
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToBeginning(Collection)
-
updateBeginningOffsets
-
seekToEnd
- Specified by:
seekToEnd
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToEnd(Collection)
-
updateEndOffsets
-
metrics
- Specified by:
metrics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.metrics()
-
partitionsFor
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String)
-
listTopics
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics()
-
updatePartitions
-
pause
- Specified by:
pause
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.pause(Collection)
-
resume
- Specified by:
resume
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.resume(Collection)
-
offsetsForTimes
public Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition,Long> timestampsToSearch)- Specified by:
offsetsForTimes
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.offsetsForTimes(Map)
-
beginningOffsets
- Specified by:
beginningOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.beginningOffsets(Collection)
-
endOffsets
- Specified by:
endOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.endOffsets(Collection)
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.close()
-
close
- Specified by:
close
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.close(Duration)
-
closed
public boolean closed() -
wakeup
public void wakeup()- Specified by:
wakeup
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.wakeup()
-
schedulePollTask
Schedule a task to be executed during a poll(). One enqueued task will be executed perpoll(Duration)
invocation. You can use this repeatedly to mock out multiple responses to poll invocations.- Parameters:
task
- the task to be executed
-
scheduleNopPollTask
public void scheduleNopPollTask() -
paused
- Specified by:
paused
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.paused()
-
partitionsFor
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String, Duration)
-
listTopics
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics(Duration)
-
offsetsForTimes
public Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition,Long> timestampsToSearch, Duration timeout)- Specified by:
offsetsForTimes
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.offsetsForTimes(Map, Duration)
-
beginningOffsets
public Map<TopicPartition,Long> beginningOffsets(Collection<TopicPartition> partitions, Duration timeout)- Specified by:
beginningOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.beginningOffsets(Collection, Duration)
-
endOffsets
public Map<TopicPartition,Long> endOffsets(Collection<TopicPartition> partitions, Duration timeout)- Specified by:
endOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.endOffsets(Collection, Duration)
-
currentLag
- Specified by:
currentLag
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.currentLag(TopicPartition)
-
groupMetadata
- Specified by:
groupMetadata
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.groupMetadata()
-
enforceRebalance
public void enforceRebalance()- Specified by:
enforceRebalance
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.enforceRebalance()
-
shouldRebalance
public boolean shouldRebalance() -
resetShouldRebalance
public void resetShouldRebalance() -
lastPollTimeout
-
setPollException(KafkaException)
instead