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
FieldsModifier and TypeFieldDescription -
Constructor Summary
ConstructorsConstructorDescriptionCreate 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(Cluster cluster, boolean autoComplete, Partitioner partitioner, Serializer<K> keySerializer, Serializer<V> valueSerializer) Create a mock producer -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidvoidclear()Clear the stored history of sent records, consumer group offsetsclientInstanceId(Duration timeout) voidclose()voidbooleanclosed()longvoidbooleanComplete the earliest uncompleted call successfully.Get the list of committed consumer group offsets since the last call toclear()voidbooleanComplete the earliest uncompleted call with the given error.voidvoidflush()booleanflushed()List<ProducerRecord<K, V>> history()Get the list of sent records since the last call toclear()voidvoidinjectTimeoutException(int injectTimeoutExceptionCounter) metrics()partitionsFor(String topic) voidsend(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.voidsendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) booleanvoidsetClientInstanceId(Uuid instanceId) voidsetMockMetrics(MetricName name, Metric metric) Set a mock metric for testing purposebooleanbooleanbooleanbooleanList<ProducerRecord<K, V>> void
-
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, 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 tonew MockProducer(Cluster.empty(), autoComplete, partitioner, keySerializer, valueSerializer) -
MockProducer
public MockProducer()Create a new mock producer with invented metadata. Equivalent tonew MockProducer(Cluster.empty(), false, null, null, null)
-
-
Method Details
-
initTransactions
public void initTransactions()Description copied from interface:Producer- Specified by:
initTransactionsin interfaceProducer<K,V>
-
beginTransaction
Description copied from interface:Producer- Specified by:
beginTransactionin interfaceProducer<K,V> - Throws:
ProducerFencedException
-
sendOffsetsToTransaction
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) throws ProducerFencedExceptionDescription copied from interface:Producer- Specified by:
sendOffsetsToTransactionin interfaceProducer<K,V> - Throws:
ProducerFencedException
-
commitTransaction
Description copied from interface:Producer- Specified by:
commitTransactionin interfaceProducer<K,V> - Throws:
ProducerFencedException
-
abortTransaction
Description copied from interface:Producer- Specified by:
abortTransactionin interfaceProducer<K,V> - Throws:
ProducerFencedException
-
send
Adds the record to the list of sent records. TheRecordMetadatareturned 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:
partitionsForin interfaceProducer<K,V>
-
disableTelemetry
public void disableTelemetry() -
injectTimeoutException
public void injectTimeoutException(int injectTimeoutExceptionCounter) - Parameters:
injectTimeoutExceptionCounter- use -1 for infinite
-
setClientInstanceId
-
clientInstanceId
Description copied from interface:Producer- Specified by:
clientInstanceIdin 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
-
addedMetrics
-
registerMetricForSubscription
- Specified by:
registerMetricForSubscriptionin interfaceProducer<K,V> - See Also:
-
unregisterMetricFromSubscription
- Specified by:
unregisterMetricFromSubscriptionin interfaceProducer<K,V> - See Also:
-