• Labs icon Lab
  • Cloud
Google Cloud Platform icon
Labs

Writing Tests for a Kafka Streams Application

Kafka Streams applications provide powerful tools for data processing, but the need to run them against a real Kafka cluster in order to exercise and test your code may be frustrating. Luckily, Kafka provides a collection of test utilities that can make the process of testing your code easier. These utilities can even allow you to unit test your streams topologies. In this lab, we will work hands-on with these test utilities by building unit tests for an existing Kafka Streams application.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Oct 18, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Clone the Starter Project from GitHub and Perform a Test Run

    1. Clone the starter project from GitHub:

      cd ~/
      git clone https://github.com/linuxacademy/content-ccdak-streams-tests-lab.git
      
    2. Perform a test run to make sure the code is able to compile and run:

      cd content-ccdak-streams-tests-lab
      ./gradlew test
      

      The code should compile, but the tests should fail since they are not implemented yet.

  2. Challenge

    Implement the Unit Tests for the `MemberSignupsStream`

    1. Edit the test class for MemberSignupsStream:

      vi src/test/java/com/linuxacademy/ccdak/streams/MemberSignupsStreamTest.java
      
    2. Implement the test_first_name test:

      @Test
      public void test_first_name() {
          // Verify that the stream accurately parses the first name from the value.
          ConsumerRecordFactory<Integer, String> factory = new ConsumerRecordFactory<>("member_signups", new IntegerSerializer(), new StringSerializer());
          ConsumerRecord<byte[], byte[]> record = factory.create("member_signups", 1, "Summers, Buffy");
          testDriver.pipeInput(record);
      
          ProducerRecord<Integer, String> outputRecord = testDriver.readOutput("member_signups_mail", new IntegerDeserializer(), new StringDeserializer());
      
          OutputVerifier.compareKeyValue(outputRecord, 1, "Buffy");
      }
      
    3. Implement the test_unknown_name_filter test:

      @Test
      public void test_unknown_name_filter() {
          // Verify that the stream filters out records with an empty name value.
          ConsumerRecordFactory<Integer, String> factory = new ConsumerRecordFactory<>("member_signups", new IntegerSerializer(), new StringSerializer());
          ConsumerRecord<byte[], byte[]> record = factory.create("member_signups", 1, "UNKNOWN");
          testDriver.pipeInput(record);
      
          ProducerRecord<Integer, String> outputRecord = testDriver.readOutput("member_signups_mail", new IntegerDeserializer(), new StringDeserializer());
      
          Assert.assertNull(outputRecord);
      }
      
    4. Implement the test_empty_name_filter test:

      @Test
      public void test_empty_name_filter() {
          // Verify that the stream filters out records with an empty name value.
          ConsumerRecordFactory<Integer, String> factory = new ConsumerRecordFactory<>("member_signups", new IntegerSerializer(), new StringSerializer());
          ConsumerRecord<byte[], byte[]> record = factory.create("member_signups", 1, "");
          testDriver.pipeInput(record);
      
          ProducerRecord<Integer, String> outputRecord = testDriver.readOutput("member_signups_mail", new IntegerDeserializer(), new StringDeserializer());
      
          Assert.assertNull(outputRecord);
      }
      
    5. Run your tests and make sure that they pass:

      ./gradlew test
      

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.