Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Writing Tests for a Kafka Consumer

You can accomplish a great deal by implementing your own Kafka consumers. Like almost any source code, it is a good idea to build unit tests to verify the functionality of your consumer code. Kafka's `MockConsumer` test fixture simplifies the process of building unit tests for producer code. In this lab, we will work with consumer test fixtures by writing a few unit tests for an existing consumer.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Intermediate
Last updated
Aug 14, 2025
Duration
45m

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-consumer-tests-lab.git
      
    2. Perform a test run to make sure the code is able to compile and run:

      cd content-ccdak-consumer-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 `MemberSignupsConsumer`
    1. Edit the test class for MemberSignupsConsumer:

      vi src/test/java/com/linuxacademy/ccdak/consumer/MemberSignupsConsumerTest.java
      
    2. Implement the testHandleRecords_output test:

      @Test
      public void testHandleRecords_output() {
          // Verify that the testHandleRecords writes the correct data to System.out
          // A text fixture called systemOutContent has already been set up in this class to capture System.out data.
          String topic = "member_signups";
          ConsumerRecord<Integer, String> record = new ConsumerRecord<>(topic, 0, 1, 2, "ROSENBERG, WILLOW");
          Map<TopicPartition, List<ConsumerRecord<Integer, String>>> records = new LinkedHashMap<>();
          records.put(new TopicPartition(topic, 0), Arrays.asList(record));
          ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records);
      
          memberSignupsConsumer.handleRecords(consumerRecords);
          Assert.assertEquals("key=2, value=ROSENBERG, WILLOW, topic=member_signups, partition=0, offset=1\n", systemOutContent.toString());
      }
      
    3. Implement the testHandleRecords_none test:

      @Test
      public void testHandleRecords_none() {
          // Verify that testHandleRecords behaves correctly when processing no records.
          // A text fixture called systemOutContent has already been set up in this class to capture System.out data.
          String topic = "member_signups";
          Map<TopicPartition, List<ConsumerRecord<Integer, String>>> records = new LinkedHashMap<>();
          records.put(new TopicPartition(topic, 0), Arrays.asList());
          ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records);
      
          memberSignupsConsumer.handleRecords(consumerRecords);
          Assert.assertEquals("", systemOutContent.toString());
      }
      
    4. Implement the testHandleRecords_multiple test:

      @Test
      public void testHandleRecords_multiple() {
          // Verify that testHandleRecords behaves correctly when processing multiple records.
          // A text fixture called systemOutContent has already been set up in this class to capture System.out data.
          String topic = "member_signups";
          ConsumerRecord<Integer, String> record1 = new ConsumerRecord<>(topic, 0, 1, 2, "ROSENBERG, WILLOW");
          ConsumerRecord<Integer, String> record2 = new ConsumerRecord<>(topic, 3, 4, 5, "HARRIS, ALEXANDER");
          Map<TopicPartition, List<ConsumerRecord<Integer, String>>> records = new LinkedHashMap<>();
          records.put(new TopicPartition(topic, 0), Arrays.asList(record1, record2));
          ConsumerRecords<Integer, String> consumerRecords = new ConsumerRecords<>(records);
      
          memberSignupsConsumer.handleRecords(consumerRecords);
          Assert.assertEquals("key=2, value=ROSENBERG, WILLOW, topic=member_signups, partition=0, offset=1\nkey=5, value=HARRIS, ALEXANDER, topic=member_signups, partition=3, offset=4\n", systemOutContent.toString());
      }
      
    5. Run your tests and make sure they pass:

      ./gradlew test
      
About the author

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.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight