Writing Tests for a Kafka Producer

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Unit testing is an important part of developing software with good practices, and this even applies to your custom Kafka producer code. Luckily, Kafka offers test fixtures that can help you easily write such tests for your Kafka producers. In this lab, we will work with Kafka’s producer test fixtures by building a few unit tests for some existing producer code.

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

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

    vi src/test/java/com/linuxacademy/ccdak/producer/MemberSignupsProducerTest.java
  2. Implement the testHandleMemberSignup_sent_data test:

    @Test
    public void testHandleMemberSignup_sent_data() {
        // Perform a simple test to verify that the producer sends the correct data to the correct topic when handleMemberSignup is called.
        // Verify that the published record has the memberId as the key and the uppercased name as the value.
        // Verify that the records is sent to the member_signups topic.
        memberSignupsProducer.handleMemberSignup(1, "Summers, Buffy");
    
        mockProducer.completeNext();
    
        List<ProducerRecord<Integer, String>> records = mockProducer.history();
        Assert.assertEquals(1, records.size());
        ProducerRecord<Integer, String> record = records.get(0);
        Assert.assertEquals(Integer.valueOf(1), record.key());
        Assert.assertEquals("SUMMERS, BUFFY", record.value());
        Assert.assertEquals("member_signups", record.topic());    
    }
  3. Implement the testHandleMemberSignup_partitioning test:

    @Test
    public void testHandleMemberSignup_partitioning() {
        // Verify that records with a value starting with A-M are assigned to partition 0, and that others are assigned to partition 1.
        // You can send two records in this test, one with a value that begins with A-M and the other that begins with N-Z.
        memberSignupsProducer.handleMemberSignup(1, "M");
        memberSignupsProducer.handleMemberSignup(1, "N");
    
        mockProducer.completeNext();
        mockProducer.completeNext();
    
        List<ProducerRecord<Integer, String>> records = mockProducer.history();
        Assert.assertEquals(2, records.size());
        ProducerRecord<Integer, String> record1 = records.get(0);
        Assert.assertEquals(Integer.valueOf(0), record1.partition());
        ProducerRecord<Integer, String> record2 = records.get(1);
        Assert.assertEquals(Integer.valueOf(1), record2.partition());
    }
  4. Implement the testHandleMemberSignup_output test:

    @Test
    public void testHandleMemberSignup_output() {
        // Verify that the producer logs the record data to System.out.
        // A text fixture called systemOutContent has already been set up in this class to capture System.out data.
        memberSignupsProducer.handleMemberSignup(1, "Summers, Buffy");
    
        mockProducer.completeNext();
    
        Assert.assertEquals("key=1, value=SUMMERS, BUFFYn", systemOutContent.toString());
    }
  5. Implement the testHandleMemberSignup_error test:

    @Test
    public void testHandleMemberSignup_error() {
        // Verify that the producer logs the error message to System.err if an error occurs when seding a record.
        // A text fixture called systemErrContent has already been set up in this class to capture System.err data.
        memberSignupsProducer.handleMemberSignup(1, "Summers, Buffy");
    
        mockProducer.errorNext(new RuntimeException("test error"));
    
        Assert.assertEquals("test errorn", systemErrContent.toString());
    }
  6. Run your tests and make sure they pass:

    ./gradlew test

Additional Resources

Your supermarket company is using Kafka to manage some of their back-end data infrastructure. They have created a membership program for customers, and a Kafka producer publishes a message to Kafka every time a new member signs up for the program.

Unfortunately, the developer who wrote the producer code had to go on sick leave before they could write unit tests for the code. Your task is to create some unit tests for the producer class.

There is a project in GitHub that contains the code. Clone this project to the Dev server. The producer class is located at src/main/java/com/linuxacademy/ccdak/producer/MemberSignupsProducer.java. You can find a test class at src/test/java/com/linuxacademy/ccdak/producer/MemberSignupsProducerTest.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.

Luckily, the developer left behind some notes on the unit tests that need to be created.

  • testHandleMemberSignup_sent_data — Call the handleMemberSignup method and test that the correct data is sent by the producer (both key and value).
  • testHandleMemberSignup_partitioning — The producer implements custom partitioning. Records where the value starts with the letters A–M are sent to partition 0, and the rest are sent to partition 1. Call handleMemberSignup twice — once with a value that starts with A–M and once with a value that starts with N–Z. Test that the partitions were set appropriately for these records.
  • testHandleMemberSignup_output — The producer implements a callback that prints the key and value of the record to System.out once the record is acknowledged. Test that the correct data is printed to System.out by the callback. A test fixture has already been set up that will allow you access data printed to System.out during the test like so: systemOutContent.toString().
  • testHandleMemberSignup_error — The producer implements a callback that prints the error message to System.err when there is an error. Make the producer return an error and test that the correct data is printed to System.err by the callback. A test fixture has already been set up that will allow you access data printed to System.err during the test like so: systemErrContent.toString().

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?