Skip to main contentIBM Automation Event-Driven Solution - Sharing knowledge

Kafka Streams Test Lab 0

Updated 03/10/2022


  • We are testing a Kafka Streams topology using Apache Kafka Streams TestDriver.
  • The topology
  • While using the TestDriver we will perform basic stateless operations and understand the testing infrastructure.

*The code for this lab is in this repository eda-kstreams-labs folder kstream-lab0

Scenario Prerequisites


  • For the purposes of this lab we suggest Java 8+


  • Maven will be needed for bootstrapping our application from the command-line and running our application.

An IDE of your choice

  • Ideally an IDE that supports Quarkus (such as Visual Studio Code)

Setting up the Quarkus Application

  • We will bootstrap the Quarkus application with the following command
quarkus create kstream-lab0
  • Since we will be using the Kafka Streams testing functionality, we will need to edit the pom.xml to add the dependency to our project. Open pom.xml and add the following:

The last dependency is for the hamcrest Domain Specific Language for test assertion.

Creating your first Test Class

  • Now let’s create our first Test Class.

  • Create the directory structure you will need for your Java file. (NOTE: If you are working in an IDE, this may be done for you when you create your package and classes.)

    mkdir -p src/test/java/ibm/eda/kstreams/lab0
  • Create a new file named src/test/java/eda/kstreams/lab0/

  • Paste the following content into the FirstKafkaStreamsTest class:

package eda.kafka.streams;
import static org.hamcrest.CoreMatchers.equalTo;
import static;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Properties;
import org.apache.kafka.common.serialization.Serdes;
  • The above code does a lot in a few lines, so we’ll walk through some of that here.
    • The @BeforeEach annotation on the buildTopology method means that it will be run each time before each test is executed, while the @AfterEach annotation on the teardown method ensures that it will be run each time after each test execution. This allows us to spin up and tear down all the necessary components to test in isolation with each test case.
    • The buildTopology method utilizes the StreamsBuilder class to construct a simple topology, reading from the input Kafka topic defined by the inTopicName String.
    • The topology, we build here, utilizes three of the stateless processors the Kafka Streams API:
      • peek allows us to look at the key and the value of the record passing through the stream and continue processing it unaffected (so we leverage this before and after the next processor used to see what is making its way through the topology)
      • filter allows us to drop records that do not meet the criteria specified (either for the key or the value). In this test class, we are filtering on any value that does not match the word "BLUE" (using a case-insensitive search)
      • to is the final processor used and to write the contents of the topology at that point to an output Kafka topic
    • The Kafka Streams Test infrastructure provides us the capability to leverage driver classes that function as their own input and output topics, removing the need from connecting directly to a live Kafka instance. The inTopic and outTopic instantiation at the bottom of the buildTopology method hooks into this test infrastructure, so that our test methods can use them to write to and read from the topology.
    • The teardown method cleans up the topology and all the data that has been sent through it for any given test run, allowing us to reset and rerun test cases as needed.
  • Build the application by running the following:
./mvnw clean verify
  • You should see output similar to the following:
[INFO] -------------------------------------------------------
[INFO] -------------------------------------------------------
[INFO] Results:
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
  • The build compiled and the test topology was successfully created. But no tests were run, because no tests were written!

Add your first Tests

  • Open src/test/java/eda/kafka/streams/ and add the following tests to the bottom of the FirstKafkaStreamsTest class:
public void isEmpty() {
assertThat(outTopic.isEmpty(), is(true));
public void isNotEmpty() {
assertThat(outTopic.isEmpty(), is(true));
inTopic.pipeInput("C01", "blue");
  • These are three simple tests:

    • The isEmpty test method checks to make sure the output topic is empty when nothing is sent through the topology
    • The isNotEmpty test method checks to make sure the output topic is not empty when an item matching our filters is sent through the topology
    • The selectBlues test method checks to make sure that our topology is filtering correctly when we send multiple items through the topology and the output topic empties correctly when the testing infrastructure reads from it.
  • You should see the tests pass with the following output:

[INFO] -------------------------------------------------------
[INFO] -------------------------------------------------------
[INFO] Running eda.kafka.streams.FirstKafkaStreamsTest
2020-09-17 09:44:33,247 INFO [io.sma.rea.mes.provider] (main) SRMSG00208: Deployment done... start processing
2020-09-17 09:44:33,250 INFO [io.sma.rea.mes.provider] (main) SRMSG00226: Found incoming connectors: [smallrye-kafka]
2020-09-17 09:44:33,251 INFO [io.sma.rea.mes.provider] (main) SRMSG00227: Found outgoing connectors: [smallrye-kafka]
2020-09-17 09:44:33,252 INFO [io.sma.rea.mes.provider] (main) SRMSG00229: Channel manager initializing...

Next Steps

  • Now that you have finished the foundational Kafka Streams testing lab, you can proceed to Lab 1 for a deeper dive into more robust real-world Kafka Streams testing use cases!