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 theConsumer
interface you can use for testing code that uses Kafka. This class is not threadsafe . However, you can use theschedulePollTask(Runnable)
method to write multithreaded tests where a driver thread waits forpoll(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 Detail
-
MockConsumer
public MockConsumer(OffsetResetStrategy offsetResetStrategy)
-
-
Method Detail
-
assignment
public Set<TopicPartition> assignment()
- Specified by:
assignment
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.assignment()
-
rebalance
public void rebalance(Collection<TopicPartition> newAssignment)
Simulate a rebalance event.
-
subscription
public Set<String> subscription()
- Specified by:
subscription
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscription()
-
subscribe
public void subscribe(Collection<String> topics)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection)
-
subscribe
public void subscribe(Pattern pattern, ConsumerRebalanceListener listener)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern, ConsumerRebalanceListener)
-
subscribe
public void subscribe(Pattern pattern)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern)
-
subscribe
public void subscribe(Collection<String> topics, ConsumerRebalanceListener listener)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection, ConsumerRebalanceListener)
-
assign
public void assign(Collection<TopicPartition> partitions)
- 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 public ConsumerRecords<K,V> poll(long timeout)
Deprecated.- Specified by:
poll
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.poll(long)
-
poll
public ConsumerRecords<K,V> poll(Duration timeout)
- Specified by:
poll
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.poll(Duration)
-
addRecord
public void addRecord(ConsumerRecord<K,V> record)
-
setException
@Deprecated public void setException(KafkaException exception)
Deprecated.UsesetPollException(KafkaException)
instead
-
setPollException
public void setPollException(KafkaException exception)
-
setOffsetsException
public void setOffsetsException(KafkaException exception)
-
commitAsync
public void commitAsync(Map<TopicPartition,OffsetAndMetadata> offsets, OffsetCommitCallback callback)
- Specified by:
commitAsync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitAsync(Map, OffsetCommitCallback)
-
commitSync
public void commitSync(Map<TopicPartition,OffsetAndMetadata> offsets)
- 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
public void commitAsync(OffsetCommitCallback callback)
- 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
public void commitSync(Duration timeout)
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Duration)
-
commitSync
public void commitSync(Map<TopicPartition,OffsetAndMetadata> offsets, Duration timeout)
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Map, Duration)
-
seek
public void seek(TopicPartition partition, long offset)
- Specified by:
seek
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seek(TopicPartition, long)
-
seek
public void seek(TopicPartition partition, OffsetAndMetadata offsetAndMetadata)
- Specified by:
seek
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seek(TopicPartition, OffsetAndMetadata)
-
committed
@Deprecated public OffsetAndMetadata committed(TopicPartition partition)
Deprecated.- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(TopicPartition)
-
committed
@Deprecated public OffsetAndMetadata committed(TopicPartition partition, Duration timeout)
Deprecated.- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(TopicPartition, Duration)
-
committed
public Map<TopicPartition,OffsetAndMetadata> committed(Set<TopicPartition> partitions)
- 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
public long position(TopicPartition partition)
- Specified by:
position
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.position(TopicPartition)
-
position
public long position(TopicPartition partition, Duration timeout)
- Specified by:
position
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.position(TopicPartition, Duration)
-
seekToBeginning
public void seekToBeginning(Collection<TopicPartition> partitions)
- Specified by:
seekToBeginning
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToBeginning(Collection)
-
updateBeginningOffsets
public void updateBeginningOffsets(Map<TopicPartition,Long> newOffsets)
-
seekToEnd
public void seekToEnd(Collection<TopicPartition> partitions)
- Specified by:
seekToEnd
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToEnd(Collection)
-
updateEndOffsets
public void updateEndOffsets(Map<TopicPartition,Long> newOffsets)
-
metrics
public Map<MetricName,? extends Metric> metrics()
- Specified by:
metrics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.metrics()
-
partitionsFor
public List<PartitionInfo> partitionsFor(String topic)
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String)
-
listTopics
public Map<String,List<PartitionInfo>> listTopics()
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics()
-
updatePartitions
public void updatePartitions(String topic, List<PartitionInfo> partitions)
-
pause
public void pause(Collection<TopicPartition> partitions)
- Specified by:
pause
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.pause(Collection)
-
resume
public void resume(Collection<TopicPartition> partitions)
- 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
public Map<TopicPartition,Long> beginningOffsets(Collection<TopicPartition> partitions)
- Specified by:
beginningOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.beginningOffsets(Collection)
-
endOffsets
public Map<TopicPartition,Long> endOffsets(Collection<TopicPartition> partitions)
- 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
@Deprecated public void close(long timeout, TimeUnit unit)
Deprecated.- Specified by:
close
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.close(long, TimeUnit)
-
closed
public boolean closed()
-
wakeup
public void wakeup()
- Specified by:
wakeup
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.wakeup()
-
schedulePollTask
public void schedulePollTask(Runnable task)
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
public Set<TopicPartition> paused()
- Specified by:
paused
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.paused()
-
partitionsFor
public List<PartitionInfo> partitionsFor(String topic, Duration timeout)
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String, Duration)
-
listTopics
public Map<String,List<PartitionInfo>> listTopics(Duration timeout)
- 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)
-
groupMetadata
public ConsumerGroupMetadata 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
public Duration lastPollTimeout()
-
-