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.

  • Constructor Details

    • MockProcessorContext

      public MockProcessorContext()
      Create a MockProcessorContext with dummy config and taskId and null stateDir. Most unit tests using this mock won't need to know the taskId, and most unit tests should be able to get by with the InMemoryKeyValueStore, so the stateDir won't matter.
    • MockProcessorContext

      public MockProcessorContext(Properties config)
      Create a MockProcessorContext with dummy taskId and null stateDir. Most unit tests using this mock won't need to know the taskId, and most unit tests should be able to get by with the InMemoryKeyValueStore, 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 a MockProcessorContext with a specified taskId and null stateDir.
      Parameters:
      config - a Properties object, used to configure the context and the processor.
      taskId - a TaskId, which the context makes available via taskId().
      stateDir - a File, which the context makes available viw stateDir().
  • Method Details

    • applicationId

      public String applicationId()
      Specified by:
      applicationId in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • taskId

      public org.apache.kafka.streams.processor.TaskId taskId()
      Specified by:
      taskId in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • appConfigs

      public Map<String,Object> appConfigs()
      Specified by:
      appConfigs in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • appConfigsWithPrefix

      public Map<String,Object> appConfigsWithPrefix(String prefix)
      Specified by:
      appConfigsWithPrefix in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • currentSystemTimeMs

      public long currentSystemTimeMs()
      Specified by:
      currentSystemTimeMs in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • currentStreamTimeMs

      public long currentStreamTimeMs()
      Specified by:
      currentStreamTimeMs in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • keySerde

      public org.apache.kafka.common.serialization.Serde<?> keySerde()
      Specified by:
      keySerde in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • valueSerde

      public org.apache.kafka.common.serialization.Serde<?> valueSerde()
      Specified by:
      valueSerde in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • stateDir

      public File stateDir()
      Specified by:
      stateDir in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • metrics

      public org.apache.kafka.streams.StreamsMetrics metrics()
      Specified by:
      metrics in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • setRecordMetadata

      public void setRecordMetadata(String topic, int partition, long offset)
      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 name
      partition - A partition number
      offset - A record offset
    • setCurrentSystemTimeMs

      public void setCurrentSystemTimeMs(long currentSystemTimeMs)
    • setCurrentStreamTimeMs

      public void setCurrentStreamTimeMs(long currentStreamTimeMs)
    • recordMetadata

      public Optional<org.apache.kafka.streams.processor.api.RecordMetadata> recordMetadata()
      Specified by:
      recordMetadata in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • getStateStore

      public <S extends org.apache.kafka.streams.processor.StateStore> S getStateStore(String name)
      Specified by:
      getStateStore in interface org.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:
      schedule in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • scheduledPunctuators

      public List<MockProcessorContext.CapturedPunctuator> scheduledPunctuators()
      Get the punctuators scheduled so far. The returned list is not affected by subsequent calls to schedule(...).
      Returns:
      A list of captured punctuators.
    • forward

      public <K extends KForward, V extends VForward> void forward(org.apache.kafka.streams.processor.api.Record<K,V> record)
      Specified by:
      forward in interface org.apache.kafka.streams.processor.api.ProcessorContext<KForward,VForward>
    • forward

      public <K extends KForward, V extends VForward> void forward(org.apache.kafka.streams.processor.api.Record<K,V> record, String childName)
      Specified by:
      forward in interface org.apache.kafka.streams.processor.api.ProcessorContext<KForward,VForward>
    • 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 to forward(...).
      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 to forward(...).
      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:
      commit in interface org.apache.kafka.streams.processor.api.ProcessingContext
    • committed

      public boolean committed()
      Whether ProcessingContext.commit() has been called in this context.
      Returns:
      true iff ProcessingContext.commit() has been called in this context since construction or reset.
    • resetCommit

      public void resetCommit()
      Reset the commit capture to false (whether or not it was previously true).
    • recordCollector

      public org.apache.kafka.streams.processor.internals.RecordCollector recordCollector()
      Specified by:
      recordCollector in interface org.apache.kafka.streams.processor.internals.RecordCollector.Supplier
    • getStateStoreContext

      public org.apache.kafka.streams.processor.StateStoreContext getStateStoreContext()
      Used to get a StateStoreContext for use with StateStore.init(StateStoreContext, StateStore) if you need to initialize a store for your tests.
      Returns:
      a StateStoreContext that delegates to this ProcessorContext.