Class MockProcessorContext<KForward,VForward>
java.lang.Object
org.apache.kafka.streams.processor.api.MockProcessorContext<KForward,VForward>
- All Implemented Interfaces:
org.apache.kafka.streams.processor.api.ProcessingContext,org.apache.kafka.streams.processor.api.ProcessorContext<KForward,,VForward> org.apache.kafka.streams.processor.internals.RecordCollector.Supplier
public class MockProcessorContext<KForward,VForward>
extends Object
implements org.apache.kafka.streams.processor.api.ProcessorContext<KForward,VForward>, org.apache.kafka.streams.processor.internals.RecordCollector.Supplier
MockProcessorContext is a mock of ProcessorContext for users to test their Processor
implementations.
The tests for this class (org.apache.kafka.streams.MockProcessorContextTest) include several behavioral tests that serve as example usage.
Note that this class does not take any automated actions (such as firing scheduled punctuators).
It simply captures any data it witnesses.
If you require more automated tests, we recommend wrapping your Processor in a minimal source-processor-sink
Topology and using the TopologyTestDriver.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final classMockProcessorContext.CapturedPunctuatorholds captured punctuators, along with their scheduling information. -
Constructor Summary
ConstructorsConstructorDescriptionMockProcessorContext(Properties config) MockProcessorContext(Properties config, org.apache.kafka.streams.processor.TaskId taskId, File stateDir) Create aMockProcessorContextwith a specified taskId and null stateDir. -
Method Summary
Modifier and TypeMethodDescription<S extends org.apache.kafka.streams.processor.StateStore>
voidaddStateStore(S stateStore) appConfigsWithPrefix(String prefix) voidcommit()booleanWhetherProcessingContext.commit()has been called in this context.longlongforward(org.apache.kafka.streams.processor.api.Record<K, V> record) List<MockProcessorContext.CapturedForward<? extends KForward, ? extends VForward>> Get all the forwarded data this context has observed.List<MockProcessorContext.CapturedForward<? extends KForward, ? extends VForward>> Get all the forwarded data this context has observed for a specific child by name.<S extends org.apache.kafka.streams.processor.StateStore>
SgetStateStore(String name) org.apache.kafka.streams.processor.StateStoreContextUsed to get aStateStoreContextfor use withStateStore.init(StateStoreContext, StateStore)if you need to initialize a store for your tests.org.apache.kafka.common.serialization.Serde<?> keySerde()org.apache.kafka.streams.StreamsMetricsmetrics()org.apache.kafka.streams.processor.internals.RecordCollectorOptional<org.apache.kafka.streams.processor.api.RecordMetadata> voidReset the commit capture tofalse(whether or not it was previouslytrue).voidClear the captured forwarded data.org.apache.kafka.streams.processor.Cancellableschedule(Duration interval, org.apache.kafka.streams.processor.PunctuationType type, org.apache.kafka.streams.processor.Punctuator callback) Get the punctuators scheduled so far.voidsetCurrentStreamTimeMs(long currentStreamTimeMs) voidsetCurrentSystemTimeMs(long currentSystemTimeMs) voidsetRecordMetadata(String topic, int partition, long offset) The context exposes these metadata for use in the processor.stateDir()org.apache.kafka.streams.processor.TaskIdtaskId()org.apache.kafka.common.serialization.Serde<?>
-
Constructor Details
-
MockProcessorContext
public MockProcessorContext()Create aMockProcessorContextwith dummyconfigandtaskIdandnullstateDir. Most unit tests using this mock won't need to know the taskId, and most unit tests should be able to get by with theInMemoryKeyValueStore, so the stateDir won't matter. -
MockProcessorContext
Create aMockProcessorContextwith dummytaskIdandnullstateDir. Most unit tests using this mock won't need to know the taskId, and most unit tests should be able to get by with theInMemoryKeyValueStore, so the stateDir won't matter.- Parameters:
config- a Properties object, used to configure the context and the processor.
-
MockProcessorContext
public MockProcessorContext(Properties config, org.apache.kafka.streams.processor.TaskId taskId, File stateDir) Create aMockProcessorContextwith a specified taskId and null stateDir.- Parameters:
config- aPropertiesobject, used to configure the context and the processor.taskId- aTaskId, which the context makes available viataskId().stateDir- aFile, which the context makes available viwstateDir().
-
-
Method Details
-
applicationId
- Specified by:
applicationIdin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
taskId
public org.apache.kafka.streams.processor.TaskId taskId()- Specified by:
taskIdin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
appConfigs
- Specified by:
appConfigsin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
appConfigsWithPrefix
- Specified by:
appConfigsWithPrefixin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
currentSystemTimeMs
public long currentSystemTimeMs()- Specified by:
currentSystemTimeMsin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
currentStreamTimeMs
public long currentStreamTimeMs()- Specified by:
currentStreamTimeMsin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
keySerde
public org.apache.kafka.common.serialization.Serde<?> keySerde()- Specified by:
keySerdein interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
valueSerde
public org.apache.kafka.common.serialization.Serde<?> valueSerde()- Specified by:
valueSerdein interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
stateDir
- Specified by:
stateDirin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
metrics
public org.apache.kafka.streams.StreamsMetrics metrics()- Specified by:
metricsin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
setRecordMetadata
The context exposes these metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set them directly.- Parameters:
topic- A topic namepartition- A partition numberoffset- A record offset
-
setCurrentSystemTimeMs
public void setCurrentSystemTimeMs(long currentSystemTimeMs) -
setCurrentStreamTimeMs
public void setCurrentStreamTimeMs(long currentStreamTimeMs) -
recordMetadata
- Specified by:
recordMetadatain interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
getStateStore
- Specified by:
getStateStorein interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
addStateStore
public <S extends org.apache.kafka.streams.processor.StateStore> void addStateStore(S stateStore) -
schedule
public org.apache.kafka.streams.processor.Cancellable schedule(Duration interval, org.apache.kafka.streams.processor.PunctuationType type, org.apache.kafka.streams.processor.Punctuator callback) - Specified by:
schedulein interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
scheduledPunctuators
Get the punctuators scheduled so far. The returned list is not affected by subsequent calls toschedule(...).- Returns:
- A list of captured punctuators.
-
forward
-
forward
-
forwarded
public List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> forwarded()Get all the forwarded data this context has observed. The returned list will not be affected by subsequent interactions with the context. The data in the list is in the same order as the calls toforward(...).- Returns:
- A list of records that were previously passed to the context.
-
forwarded
public List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> forwarded(String childName) Get all the forwarded data this context has observed for a specific child by name. The returned list will not be affected by subsequent interactions with the context. The data in the list is in the same order as the calls toforward(...).- Parameters:
childName- The child name to retrieve forwards for- Returns:
- A list of records that were previously passed to the context.
-
resetForwards
public void resetForwards()Clear the captured forwarded data. -
commit
public void commit()- Specified by:
commitin interfaceorg.apache.kafka.streams.processor.api.ProcessingContext
-
committed
public boolean committed()WhetherProcessingContext.commit()has been called in this context.- Returns:
trueiffProcessingContext.commit()has been called in this context since construction or reset.
-
resetCommit
public void resetCommit()Reset the commit capture tofalse(whether or not it was previouslytrue). -
recordCollector
public org.apache.kafka.streams.processor.internals.RecordCollector recordCollector()- Specified by:
recordCollectorin interfaceorg.apache.kafka.streams.processor.internals.RecordCollector.Supplier
-
getStateStoreContext
public org.apache.kafka.streams.processor.StateStoreContext getStateStoreContext()Used to get aStateStoreContextfor use withStateStore.init(StateStoreContext, StateStore)if you need to initialize a store for your tests.- Returns:
- a
StateStoreContextthat delegates to this ProcessorContext.
-