Class MockProducer<K,V>
- java.lang.Object
 - 
- org.apache.kafka.clients.producer.MockProducer<K,V>
 
 
- 
- All Implemented Interfaces:
 java.io.Closeable,java.lang.AutoCloseable,Producer<K,V>
public class MockProducer<K,V> extends java.lang.Object implements Producer<K,V>
This class is not supported.
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
Fields Modifier and Type Field Description java.lang.RuntimeExceptionabortTransactionExceptionjava.lang.RuntimeExceptionbeginTransactionExceptionjava.lang.RuntimeExceptioncloseExceptionjava.lang.RuntimeExceptioncommitTransactionExceptionjava.lang.RuntimeExceptionflushExceptionjava.lang.RuntimeExceptioninitTransactionExceptionjava.lang.RuntimeExceptionpartitionsForExceptionjava.lang.RuntimeExceptionsendExceptionjava.lang.RuntimeExceptionsendOffsetsToTransactionException 
- 
Constructor Summary
Constructors Constructor Description MockProducer()Create 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabortTransaction()voidbeginTransaction()voidclear()Clear the stored history of sent records, consumer group offsetsvoidclose()voidclose(java.time.Duration timeout)booleanclosed()longcommitCount()voidcommitTransaction()booleancompleteNext()Complete the earliest uncompleted call successfully.java.util.List<java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>>>consumerGroupOffsetsHistory()Get the list of committed consumer group offsets since the last call toclear()booleanerrorNext(java.lang.RuntimeException e)Complete the earliest uncompleted call with the given error.voidfenceProducer()voidflush()booleanflushed()java.util.List<ProducerRecord<K,V>>history()Get the list of sent records since the last call toclear()voidinitTransactions()java.util.Map<MetricName,Metric>metrics()java.util.List<PartitionInfo>partitionsFor(java.lang.String topic)java.util.concurrent.Future<RecordMetadata>send(ProducerRecord<K,V> record)Adds the record to the list of sent records.java.util.concurrent.Future<RecordMetadata>send(ProducerRecord<K,V> record, Callback callback)Adds the record to the list of sent records.voidsendOffsetsToTransaction(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.lang.String consumerGroupId)voidsendOffsetsToTransaction(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata)booleansentOffsets()voidsetMockMetrics(MetricName name, Metric metric)Set a mock metric for testing purposebooleantransactionAborted()booleantransactionCommitted()booleantransactionInFlight()booleantransactionInitialized()java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>>uncommittedOffsets()java.util.List<ProducerRecord<K,V>>uncommittedRecords() 
 - 
 
- 
- 
Field Detail
- 
initTransactionException
public java.lang.RuntimeException initTransactionException
 
- 
beginTransactionException
public java.lang.RuntimeException beginTransactionException
 
- 
sendOffsetsToTransactionException
public java.lang.RuntimeException sendOffsetsToTransactionException
 
- 
commitTransactionException
public java.lang.RuntimeException commitTransactionException
 
- 
abortTransactionException
public java.lang.RuntimeException abortTransactionException
 
- 
sendException
public java.lang.RuntimeException sendException
 
- 
flushException
public java.lang.RuntimeException flushException
 
- 
partitionsForException
public java.lang.RuntimeException partitionsForException
 
- 
closeException
public java.lang.RuntimeException closeException
 
 - 
 
- 
Constructor Detail
- 
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 Detail
- 
initTransactions
public void initTransactions()
Description copied from interface:Producer- Specified by:
 initTransactionsin interfaceProducer<K,V>
 
- 
beginTransaction
public void beginTransaction() throws ProducerFencedExceptionDescription copied from interface:Producer- Specified by:
 beginTransactionin interfaceProducer<K,V>- Throws:
 ProducerFencedException
 
- 
sendOffsetsToTransaction
public void sendOffsetsToTransaction(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, java.lang.String consumerGroupId) throws ProducerFencedException
Description copied from interface:Producer- Specified by:
 sendOffsetsToTransactionin interfaceProducer<K,V>- Throws:
 ProducerFencedException
 
- 
sendOffsetsToTransaction
public void sendOffsetsToTransaction(java.util.Map<TopicPartition,OffsetAndMetadata> offsets, ConsumerGroupMetadata groupMetadata) throws ProducerFencedException
Description copied from interface:Producer- Specified by:
 sendOffsetsToTransactionin interfaceProducer<K,V>- Throws:
 ProducerFencedException
 
- 
commitTransaction
public void commitTransaction() throws ProducerFencedExceptionDescription copied from interface:Producer- Specified by:
 commitTransactionin interfaceProducer<K,V>- Throws:
 ProducerFencedException
 
- 
abortTransaction
public void abortTransaction() throws ProducerFencedExceptionDescription copied from interface:Producer- Specified by:
 abortTransactionin interfaceProducer<K,V>- Throws:
 ProducerFencedException
 
- 
send
public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record)
Adds the record to the list of sent records. TheRecordMetadatareturned will be immediately satisfied. 
- 
send
public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record, Callback callback)
Adds the record to the list of sent records. 
- 
flush
public void flush()
Description copied from interface:Producer 
- 
partitionsFor
public java.util.List<PartitionInfo> partitionsFor(java.lang.String topic)
Description copied from interface:Producer- Specified by:
 partitionsForin interfaceProducer<K,V>
 
- 
metrics
public java.util.Map<MetricName,Metric> metrics()
Description copied from interface:Producer 
- 
setMockMetrics
public void setMockMetrics(MetricName name, Metric metric)
Set a mock metric for testing purpose 
- 
close
public void close()
Description copied from interface:Producer 
- 
close
public void close(java.time.Duration timeout)
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
public java.util.List<ProducerRecord<K,V>> history()
Get the list of sent records since the last call toclear() 
- 
uncommittedRecords
public java.util.List<ProducerRecord<K,V>> uncommittedRecords()
 
- 
consumerGroupOffsetsHistory
public java.util.List<java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>>> consumerGroupOffsetsHistory()
Get the list of committed consumer group offsets since the last call toclear() 
- 
uncommittedOffsets
public java.util.Map<java.lang.String,java.util.Map<TopicPartition,OffsetAndMetadata>> 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
public boolean errorNext(java.lang.RuntimeException e)
Complete the earliest uncompleted call with the given error.- Returns:
 - true if there was an uncompleted call to complete
 
 
 - 
 
 -