Class MockConsumer<K,V>
java.lang.Object
org.apache.kafka.clients.consumer.MockConsumer<K,V>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addRecord
(ConsumerRecord<K, V> record) void
assign
(Collection<TopicPartition> partitions) beginningOffsets
(Collection<TopicPartition> partitions) beginningOffsets
(Collection<TopicPartition> partitions, Duration timeout) clientInstanceId
(Duration timeout) void
close()
void
boolean
closed()
void
void
commitAsync
(Map<TopicPartition, OffsetAndMetadata> offsets, OffsetCommitCallback callback) void
commitAsync
(OffsetCommitCallback callback) void
void
commitSync
(Duration timeout) void
commitSync
(Map<TopicPartition, OffsetAndMetadata> offsets) void
commitSync
(Map<TopicPartition, OffsetAndMetadata> offsets, Duration timeout) committed
(Set<TopicPartition> partitions) committed
(Set<TopicPartition> partitions, Duration timeout) committed
(TopicPartition partition) Deprecated.committed
(TopicPartition partition, Duration timeout) Deprecated.currentLag
(TopicPartition topicPartition) void
endOffsets
(Collection<TopicPartition> partitions) endOffsets
(Collection<TopicPartition> partitions, Duration timeout) void
void
enforceRebalance
(String reason) void
injectTimeoutException
(int injectTimeoutExceptionCounter) listTopics
(Duration timeout) Map<MetricName,
? extends Metric> metrics()
offsetsForTimes
(Map<TopicPartition, Long> timestampsToSearch) offsetsForTimes
(Map<TopicPartition, Long> timestampsToSearch, Duration timeout) partitionsFor
(String topic) partitionsFor
(String topic, Duration timeout) void
pause
(Collection<TopicPartition> partitions) paused()
poll
(long timeout) Deprecated.long
position
(TopicPartition partition) long
position
(TopicPartition partition, Duration timeout) void
rebalance
(Collection<TopicPartition> newAssignment) Simulate a rebalance event.void
void
resume
(Collection<TopicPartition> partitions) void
void
schedulePollTask
(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
(Collection<TopicPartition> partitions) void
seekToEnd
(Collection<TopicPartition> partitions) void
setClientInstanceId
(Uuid instanceId) void
setException
(KafkaException exception) Deprecated.void
setOffsetsException
(KafkaException exception) void
setPollException
(KafkaException exception) boolean
void
subscribe
(Collection<String> topics) void
subscribe
(Collection<String> topics, ConsumerRebalanceListener listener) void
void
subscribe
(Pattern pattern, ConsumerRebalanceListener listener) void
void
updateBeginningOffsets
(Map<TopicPartition, Long> newOffsets) void
updateEndOffsets
(Map<TopicPartition, Long> newOffsets) void
updatePartitions
(String topic, List<PartitionInfo> partitions) void
wakeup()
-
Constructor Details
-
MockConsumer
-
-
Method Details
-
assignment
- Specified by:
assignment
in interfaceConsumer<K,
V> - See Also:
-
rebalance
Simulate a rebalance event. -
subscription
- Specified by:
subscription
in interfaceConsumer<K,
V> - See Also:
-
subscribe
-
subscribe
-
subscribe
-
subscribe
-
assign
-
unsubscribe
public void unsubscribe()- Specified by:
unsubscribe
in interfaceConsumer<K,
V> - See Also:
-
poll
Deprecated. -
poll
-
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:
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,
V> - See Also:
-
commitAsync
public void commitAsync()- Specified by:
commitAsync
in interfaceConsumer<K,
V> - See Also:
-
commitAsync
- Specified by:
commitAsync
in interfaceConsumer<K,
V> - See Also:
-
commitSync
public void commitSync()- Specified by:
commitSync
in interfaceConsumer<K,
V> - See Also:
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,
V> - See Also:
-
commitSync
- Specified by:
commitSync
in interfaceConsumer<K,
V> - See Also:
-
seek
-
seek
-
committed
Deprecated. -
committed
Deprecated. -
committed
-
committed
public Map<TopicPartition,OffsetAndMetadata> committed(Set<TopicPartition> partitions, Duration timeout) -
position
-
position
-
seekToBeginning
- Specified by:
seekToBeginning
in interfaceConsumer<K,
V> - See Also:
-
updateBeginningOffsets
-
seekToEnd
-
updateEndOffsets
-
disableTelemetry
public void disableTelemetry() -
injectTimeoutException
public void injectTimeoutException(int injectTimeoutExceptionCounter) - Parameters:
injectTimeoutExceptionCounter
- use -1 for infinite
-
setClientInstanceId
-
clientInstanceId
Description copied from interface:Consumer
- Specified by:
clientInstanceId
in interfaceConsumer<K,
V>
-
metrics
-
partitionsFor
- Specified by:
partitionsFor
in interfaceConsumer<K,
V> - See Also:
-
listTopics
- Specified by:
listTopics
in interfaceConsumer<K,
V> - See Also:
-
updatePartitions
-
pause
-
resume
-
offsetsForTimes
public Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch) - Specified by:
offsetsForTimes
in interfaceConsumer<K,
V> - See Also:
-
beginningOffsets
- Specified by:
beginningOffsets
in interfaceConsumer<K,
V> - See Also:
-
endOffsets
- Specified by:
endOffsets
in interfaceConsumer<K,
V> - See Also:
-
close
public void close() -
close
-
closed
public boolean closed() -
wakeup
public void 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
-
partitionsFor
- Specified by:
partitionsFor
in interfaceConsumer<K,
V> - See Also:
-
listTopics
- Specified by:
listTopics
in interfaceConsumer<K,
V> - See Also:
-
offsetsForTimes
public Map<TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch, Duration timeout) - Specified by:
offsetsForTimes
in interfaceConsumer<K,
V> - See Also:
-
beginningOffsets
public Map<TopicPartition,Long> beginningOffsets(Collection<TopicPartition> partitions, Duration timeout) - Specified by:
beginningOffsets
in interfaceConsumer<K,
V> - See Also:
-
endOffsets
- Specified by:
endOffsets
in interfaceConsumer<K,
V> - See Also:
-
currentLag
- Specified by:
currentLag
in interfaceConsumer<K,
V> - See Also:
-
groupMetadata
- Specified by:
groupMetadata
in interfaceConsumer<K,
V> - See Also:
-
enforceRebalance
public void enforceRebalance()- Specified by:
enforceRebalance
in interfaceConsumer<K,
V> - See Also:
-
enforceRebalance
- Specified by:
enforceRebalance
in interfaceConsumer<K,
V> - See Also:
-
shouldRebalance
public boolean shouldRebalance() -
resetShouldRebalance
public void resetShouldRebalance() -
lastPollTimeout
-
setPollException(KafkaException)
instead