Class MockConsumer<K,V>
- java.lang.Object
-
- org.apache.kafka.clients.consumer.MockConsumer<K,V>
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Consumer<K,V>
public class MockConsumer<K,V> extends java.lang.Object implements Consumer<K,V>
This class is not supported.
A mock of the
Consumer
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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addRecord(ConsumerRecord<K,V> record)
void
assign(java.util.Collection<TopicPartition> partitions)
void
assign(java.util.List<TopicPartition> partitions)
java.util.Set<TopicPartition>
assignment()
java.util.Map<TopicPartition,java.lang.Long>
beginningOffsets(java.util.Collection<TopicPartition> partitions)
java.util.Map<TopicPartition,java.lang.Long>
beginningOffsets(java.util.Collection<TopicPartition> partitions, java.time.Duration timeout)
void
close()
void
close(long timeout, java.util.concurrent.TimeUnit unit)
Deprecated.void
close(java.time.Duration timeout)
boolean
closed()
void
commitAsync()
void
commitAsync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, OffsetCommitCallback callback)
void
commitAsync(OffsetCommitCallback callback)
void
commitSync()
void
commitSync(java.time.Duration timeout)
void
commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets)
void
commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.time.Duration timeout)
java.util.Map<TopicPartition,OffsetAndMetadata>
committed(java.util.Set<TopicPartition> partitions)
java.util.Map<TopicPartition,OffsetAndMetadata>
committed(java.util.Set<TopicPartition> partitions, java.time.Duration timeout)
OffsetAndMetadata
committed(TopicPartition partition)
Deprecated.OffsetAndMetadata
committed(TopicPartition partition, java.time.Duration timeout)
Deprecated.java.util.Map<TopicPartition,java.lang.Long>
endOffsets(java.util.Collection<TopicPartition> partitions)
java.util.Map<TopicPartition,java.lang.Long>
endOffsets(java.util.Collection<TopicPartition> partitions, java.time.Duration timeout)
void
enforceRebalance()
ConsumerGroupMetadata
groupMetadata()
java.time.Duration
lastPollTimeout()
java.util.Map<java.lang.String,java.util.List<PartitionInfo>>
listTopics()
java.util.Map<java.lang.String,java.util.List<PartitionInfo>>
listTopics(java.lang.String stream)
java.util.Map<java.lang.String,java.util.List<PartitionInfo>>
listTopics(java.lang.String stream, java.time.Duration timeout)
java.util.Map<java.lang.String,java.util.List<PartitionInfo>>
listTopics(java.time.Duration timeout)
java.util.Map<java.lang.String,java.util.List<PartitionInfo>>
listTopics(java.util.regex.Pattern pattern)
java.util.Map<MetricName,? extends Metric>
metrics()
java.util.Map<TopicPartition,OffsetAndTimestamp>
offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch)
java.util.Map<TopicPartition,OffsetAndTimestamp>
offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch, java.time.Duration timeout)
java.util.List<PartitionInfo>
partitionsFor(java.lang.String topic)
java.util.List<PartitionInfo>
partitionsFor(java.lang.String topic, java.time.Duration timeout)
void
pause(java.util.Collection<TopicPartition> partitions)
void
pause(TopicPartition... partitions)
Deprecated.java.util.Set<TopicPartition>
paused()
ConsumerRecords<K,V>
poll(long timeout)
Deprecated.ConsumerRecords<K,V>
poll(java.time.Duration timeout)
long
position(TopicPartition partition)
long
position(TopicPartition partition, java.time.Duration timeout)
void
rebalance(java.util.Collection<TopicPartition> newAssignment)
Simulate a rebalance event.void
resetShouldRebalance()
void
resume(java.util.Collection<TopicPartition> partitions)
void
resume(TopicPartition... partitions)
Deprecated.void
scheduleNopPollTask()
void
schedulePollTask(java.lang.Runnable task)
Schedule a task to be executed during a poll().void
seek(TopicPartition partition, long offset)
void
seek(TopicPartition partition, OffsetAndMetadata offsetAndMetadata)
void
seekToBeginning(java.util.Collection<TopicPartition> partitions)
void
seekToBeginning(TopicPartition... partitions)
Deprecated.void
seekToEnd(java.util.Collection<TopicPartition> partitions)
void
seekToEnd(TopicPartition... partitions)
Deprecated.void
setException(KafkaException exception)
Deprecated.UsesetPollException(KafkaException)
insteadvoid
setOffsetsException(KafkaException exception)
void
setPollException(KafkaException exception)
boolean
shouldRebalance()
void
subscribe(java.util.Collection<java.lang.String> topics)
void
subscribe(java.util.Collection<java.lang.String> topics, ConsumerRebalanceListener listener)
void
subscribe(java.util.List<java.lang.String> topics)
void
subscribe(java.util.List<java.lang.String> topics, ConsumerRebalanceListener listener)
void
subscribe(java.util.regex.Pattern pattern)
void
subscribe(java.util.regex.Pattern pattern, ConsumerRebalanceListener listener)
java.util.Set<java.lang.String>
subscription()
void
unsubscribe()
void
updateBeginningOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
void
updateEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
void
updatePartitions(java.lang.String topic, java.util.List<PartitionInfo> partitions)
void
wakeup()
-
-
-
Constructor Detail
-
MockConsumer
public MockConsumer(OffsetResetStrategy offsetResetStrategy)
-
-
Method Detail
-
assignment
public java.util.Set<TopicPartition> assignment()
- Specified by:
assignment
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.assignment()
-
rebalance
public void rebalance(java.util.Collection<TopicPartition> newAssignment)
Simulate a rebalance event.
-
subscription
public java.util.Set<java.lang.String> subscription()
- Specified by:
subscription
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscription()
-
subscribe
public void subscribe(java.util.Collection<java.lang.String> topics)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection)
-
subscribe
public void subscribe(java.util.List<java.lang.String> topics)
-
subscribe
public void subscribe(java.util.regex.Pattern pattern, ConsumerRebalanceListener listener)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern, ConsumerRebalanceListener)
-
subscribe
public void subscribe(java.util.regex.Pattern pattern)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Pattern)
-
subscribe
public void subscribe(java.util.Collection<java.lang.String> topics, ConsumerRebalanceListener listener)
- Specified by:
subscribe
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.subscribe(Collection, ConsumerRebalanceListener)
-
subscribe
public void subscribe(java.util.List<java.lang.String> topics, ConsumerRebalanceListener listener)
-
assign
public void assign(java.util.Collection<TopicPartition> partitions)
- Specified by:
assign
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.assign(Collection)
-
assign
public void assign(java.util.List<TopicPartition> partitions)
- Specified by:
assign
in interfaceConsumer<K,V>
- See Also:
Retained for backward binary compactibility
-
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(java.time.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(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, OffsetCommitCallback callback)
- Specified by:
commitAsync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitAsync(Map, OffsetCommitCallback)
-
commitSync
public void commitSync(java.util.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(java.time.Duration timeout)
- Specified by:
commitSync
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.commitSync(Duration)
-
commitSync
public void commitSync(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.time.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, java.time.Duration timeout)
Deprecated.- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(TopicPartition, Duration)
-
committed
public java.util.Map<TopicPartition,OffsetAndMetadata> committed(java.util.Set<TopicPartition> partitions)
- Specified by:
committed
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.committed(Set)
-
committed
public java.util.Map<TopicPartition,OffsetAndMetadata> committed(java.util.Set<TopicPartition> partitions, java.time.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, java.time.Duration timeout)
- Specified by:
position
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.position(TopicPartition, Duration)
-
seekToBeginning
public void seekToBeginning(java.util.Collection<TopicPartition> partitions)
- Specified by:
seekToBeginning
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToBeginning(Collection)
-
seekToBeginning
@Deprecated public void seekToBeginning(TopicPartition... partitions)
Deprecated.- Specified by:
seekToBeginning
in interfaceConsumer<K,V>
-
updateBeginningOffsets
public void updateBeginningOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
-
seekToEnd
public void seekToEnd(java.util.Collection<TopicPartition> partitions)
- Specified by:
seekToEnd
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.seekToEnd(Collection)
-
seekToEnd
@Deprecated public void seekToEnd(TopicPartition... partitions)
Deprecated.
-
updateEndOffsets
public void updateEndOffsets(java.util.Map<TopicPartition,java.lang.Long> newOffsets)
-
metrics
public java.util.Map<MetricName,? extends Metric> metrics()
- Specified by:
metrics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.metrics()
-
partitionsFor
public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String)
-
listTopics
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics()
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics()
-
listTopics
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics(java.lang.String stream)
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics(String)
-
listTopics
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics(java.lang.String stream, java.time.Duration timeout)
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics(String, Duration)
-
listTopics
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics(java.util.regex.Pattern pattern)
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics(Pattern)
-
updatePartitions
public void updatePartitions(java.lang.String topic, java.util.List<PartitionInfo> partitions)
-
pause
public void pause(java.util.Collection<TopicPartition> partitions)
- Specified by:
pause
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.pause(Collection)
-
pause
@Deprecated public void pause(TopicPartition... partitions)
Deprecated.
-
resume
public void resume(java.util.Collection<TopicPartition> partitions)
- Specified by:
resume
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.resume(Collection)
-
resume
@Deprecated public void resume(TopicPartition... partitions)
Deprecated.
-
offsetsForTimes
public java.util.Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch)
- Specified by:
offsetsForTimes
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.offsetsForTimes(Map)
-
beginningOffsets
public java.util.Map<TopicPartition,java.lang.Long> beginningOffsets(java.util.Collection<TopicPartition> partitions)
- Specified by:
beginningOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.beginningOffsets(Collection)
-
endOffsets
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions)
- Specified by:
endOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.endOffsets(Collection)
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.close()
-
close
@Deprecated public void close(long timeout, java.util.concurrent.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(java.lang.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 java.util.Set<TopicPartition> paused()
- Specified by:
paused
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.paused()
-
partitionsFor
public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic, java.time.Duration timeout)
- Specified by:
partitionsFor
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.partitionsFor(String, Duration)
-
listTopics
public java.util.Map<java.lang.String,java.util.List<PartitionInfo>> listTopics(java.time.Duration timeout)
- Specified by:
listTopics
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.listTopics(Duration)
-
offsetsForTimes
public java.util.Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(java.util.Map<TopicPartition,java.lang.Long> timestampsToSearch, java.time.Duration timeout)
- Specified by:
offsetsForTimes
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.offsetsForTimes(Map, Duration)
-
beginningOffsets
public java.util.Map<TopicPartition,java.lang.Long> beginningOffsets(java.util.Collection<TopicPartition> partitions, java.time.Duration timeout)
- Specified by:
beginningOffsets
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.beginningOffsets(Collection, Duration)
-
endOffsets
public java.util.Map<TopicPartition,java.lang.Long> endOffsets(java.util.Collection<TopicPartition> partitions, java.time.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 java.time.Duration lastPollTimeout()
-
close
public void close(java.time.Duration timeout)
- Specified by:
close
in interfaceConsumer<K,V>
- See Also:
KafkaConsumer.close(Duration)
-
-