Class MockProducer<K,V>
java.lang.Object
org.apache.kafka.clients.producer.MockProducer<K,V>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Producer<K,
V>
A mock of the producer interface you can use for testing code that uses Kafka.
By default this mock will synchronously complete each send call successfully. However it can be configured to allow the user to control the completion of the call and supply an optional error for the producer to throw.
-
Field Summary
Modifier and TypeFieldDescription -
Constructor Summary
ConstructorDescriptionCreate a new mock producer with invented metadata.MockProducer
(boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a new mock producer with invented metadata the given autoComplete setting, partitioner and key\value serializers.MockProducer
(boolean autoComplete, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a new mock producer with invented metadata the given autoComplete setting and key\value serializers.MockProducer
(Cluster cluster, boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a mock producer -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
clear()
Clear the stored history of sent records, consumer group offsetsvoid
close()
void
boolean
closed()
long
void
boolean
Complete the earliest uncompleted call successfully.Get the list of committed consumer group offsets since the last call toclear()
boolean
Complete the earliest uncompleted call with the given error.void
void
flush()
boolean
flushed()
List<ProducerRecord<K,
V>> history()
Get the list of sent records since the last call toclear()
void
metrics()
partitionsFor
(String topic) send
(ProducerRecord<K, V> record) Adds the record to the list of sent records.send
(ProducerRecord<K, V> record, Callback callback) Adds the record to the list of sent records.void
sendOffsetsToTransaction
(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) Deprecated.void
sendOffsetsToTransaction
(Map<TopicPartition, OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) boolean
void
setMockMetrics
(MetricName name, Metric metric) Set a mock metric for testing purposeboolean
boolean
boolean
boolean
List<ProducerRecord<K,
V>>
-
Field Details
-
initTransactionException
-
beginTransactionException
-
sendOffsetsToTransactionException
-
commitTransactionException
-
abortTransactionException
-
sendException
-
flushException
-
partitionsForException
-
closeException
-
-
Constructor Details
-
MockProducer
public MockProducer(Cluster cluster, boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a mock producer- Parameters:
cluster
- The cluster holding metadata for this producerautoComplete
- If true automatically complete all requests successfully and execute the callback. Otherwise the user must callcompleteNext()
orerrorNext(RuntimeException)
aftersend()
to complete the call and unblock theFuture<RecordMetadata>
that is returned.partitioner
- The partition strategykeySerializer
- The serializer for key that implementsSerializer
.valueSerializer
- The serializer for value that implementsSerializer
.
-
MockProducer
public MockProducer(boolean autoComplete, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a new mock producer with invented metadata the given autoComplete setting and key\value serializers. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), autoComplete, new DefaultPartitioner(), keySerializer, valueSerializer)} -
MockProducer
public MockProducer(boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a new mock producer with invented metadata the given autoComplete setting, partitioner and key\value serializers. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), autoComplete, partitioner, keySerializer, valueSerializer)} -
MockProducer
public MockProducer()Create a new mock producer with invented metadata. Equivalent toMockProducer(Cluster, boolean, Partitioner, Serializer, Serializer)
new MockProducer(Cluster.empty(), false, null, null, null)}
-
-
Method Details
-
initTransactions
public void initTransactions()Description copied from interface:Producer
- Specified by:
initTransactions
in interfaceProducer<K,
V>
-
beginTransaction
Description copied from interface:Producer
- Specified by:
beginTransaction
in interfaceProducer<K,
V> - Throws:
ProducerFencedException
-
sendOffsetsToTransaction
@Deprecated public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedExceptionDeprecated.Description copied from interface:Producer
- Specified by:
sendOffsetsToTransaction
in interfaceProducer<K,
V> - Throws:
ProducerFencedException
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) throws ProducerFencedExceptionDescription copied from interface:Producer
- Specified by:
sendOffsetsToTransaction
in interfaceProducer<K,
V> - Throws:
ProducerFencedException
-
commitTransaction
Description copied from interface:Producer
- Specified by:
commitTransaction
in interfaceProducer<K,
V> - Throws:
ProducerFencedException
-
abortTransaction
Description copied from interface:Producer
- Specified by:
abortTransaction
in interfaceProducer<K,
V> - Throws:
ProducerFencedException
-
send
Adds the record to the list of sent records. TheRecordMetadata
returned will be immediately satisfied. -
send
Adds the record to the list of sent records. -
flush
public void flush()Description copied from interface:Producer
-
partitionsFor
Description copied from interface:Producer
- Specified by:
partitionsFor
in interfaceProducer<K,
V>
-
metrics
Description copied from interface:Producer
-
setMockMetrics
Set a mock metric for testing purpose -
close
public void close()Description copied from interface:Producer
-
close
Description copied from interface:Producer
-
closed
public boolean closed() -
fenceProducer
public void fenceProducer() -
transactionInitialized
public boolean transactionInitialized() -
transactionInFlight
public boolean transactionInFlight() -
transactionCommitted
public boolean transactionCommitted() -
transactionAborted
public boolean transactionAborted() -
flushed
public boolean flushed() -
sentOffsets
public boolean sentOffsets() -
commitCount
public long commitCount() -
history
Get the list of sent records since the last call toclear()
-
uncommittedRecords
-
consumerGroupOffsetsHistory
Get the list of committed consumer group offsets since the last call toclear()
-
uncommittedOffsets
-
clear
public void clear()Clear the stored history of sent records, consumer group offsets -
completeNext
public boolean completeNext()Complete the earliest uncompleted call successfully.- Returns:
- true if there was an uncompleted call to complete
-
errorNext
Complete the earliest uncompleted call with the given error.- Returns:
- true if there was an uncompleted call to complete
-