Writing Tests for a Kafka Consumer

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

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.

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=1n", 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=1nkey=5, value=HARRIS, ALEXANDER, topic=member_signups, partition=3, offset=4n", systemOutContent.toString());
    }
  5. Run your tests and make sure they pass:

    ./gradlew test

Additional Resources

Your supermarket company has a consumer that consumes messages that are created when customers sign up for a membership program. This consumer simply logs the messages and their metadata to the console.

The company is reviewing the codebase for compliance with good practices, and this consumer has no unit tests. Your task is to write some unit tests for the consumer.

There is a project in GitHub that contains the code. Clone this project to the Dev server. The consumer class is located at src/main/java/com/linuxacademy/ccdak/consumer/MemberSignupsConsumer.java. You can find a test class at src/test/java/com/linuxacademy/ccdak/consumer/MemberSignupsConsumerTest.java. Edit the test class and implement your unit tests there. There are already test methods and some test fixtures set up in the class.

Note the test class contains a test fixture called systemOutContent. You can use this to access data written to System.out during the test like so:

systemOutContent.toString()

Here are some notes on the tests that need to be created:

  • testHandleRecords_output — This test should simply verify the output. Call the handleRecords and pass in a record. Test that the data is written to System.out in the expected format.
  • testHandleRecords_none — Test that handleRecords works as expected when the collection of records is empty.
  • testHandleRecords_multiple — Test that handleRecords works as expected when the collection of records contains more than one record.

If you get stuck, feel free to check out the solution video or the detailed instructions under each objective. Good luck!

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?