paint-brush
How to Simulate the Kafka Service Using the Citrus Frameworkby@taacoder
361 reads
361 reads

How to Simulate the Kafka Service Using the Citrus Framework

by Alexey TaranenkoJanuary 11th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Today, many teams use Kafka in service development. Many leading IT companies are already actively and successfully using this tool. There are probably already many articles about the advantages of using Kafka on projects, but now I want to talk about this in the context of testing, or rather, how a tester can create Kafka-mock, that is, a simulator that can be used in tests.
featured image - How to Simulate the Kafka Service Using the Citrus Framework
Alexey Taranenko HackerNoon profile picture

Today, many teams use Kafka in service development. Many leading IT companies are already actively and successfully using this tool. There are probably already many articles about the advantages of using Kafka on projects, but now I want to talk about this in the context of testing, or rather, how a tester can create Kafka-mock, that is, a simulator that can be used in tests.


A bit of clarity, let's see the interaction scheme, where there is Kafka and the system under testing, suppose it looks like this:

Hence, if we want to test SUT(system under test ) in isolation, then we need the following stubs:

  • Kafka mock itself
  • Producer mock
  • Сonsumer mock

Knowing all this, let's start preparing a test environment in a test project. I will be using Citrus 3.2.1 version settings. I talked about how to generate a template Citrus project in one of the previous articles, now let's immediately start configuring the pom.xml and citrus-context.xml files:


1. Add dependency to pom.xml

    <dependency>
      <groupId>com.consol.citrus</groupId>
      <artifactId>citrus-kafka</artifactId>
      <version>${citrus.version}</version>
    </dependency>


2. Add namespace and schemaLocation for Kafka to citrus-context.xml

xmlns:citrus-kafka="http://www.citrusframework.org/schema/kafka/config"

xsi:schemaLocation=
http://www.citrusframework.org/schema/kafka/config 
http://www.citrusframework.org/schema/kafka/config/citrus-kafka-config.xsd


3. Create a Kafka embedded bean

    <citrus-kafka:embedded-server id="embeddedKafka"
                                  kafka-server-port="9590"
                                  topics="QUEUE_IN_NAME, QUEUE_OUT_NAME"
                                  partitions="10"/>


4. Create a Producer bean

    <citrus-kafka:endpoint id="mqProducer"
                           server="localhost:9590"
                           topic="QUEUE_IN_NAME"/>


5. Create a Consumer bean

    <citrus-kafka:endpoint id="mqConsumer"
                           server="localhost:9590"
                           topic="QUEUE_OUT_NAME"/>


Now the environment configuration is ready and you can start writing a test that will send a message as an MQ Producer and after working through the SUT, the test MQ Consumer will receive a message that will be checked for the expected result.


So, a Citrus test would look like this:

package tests;

import com.consol.citrus.annotations.CitrusTest;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.testng.TestNGCitrusSupport;
import org.testng.annotations.Test;

import static com.consol.citrus.actions.ReceiveMessageAction.Builder.receive;
import static com.consol.citrus.actions.SendMessageAction.Builder.send;

public class TestKafka extends TestNGCitrusSupport {

    public TestContext context;

    @Test(description = "Kafka test")
    @CitrusTest
    public void getTestActions() {
        this.context = citrus.getCitrusContext().createTestContext();

        run(send("mqProducer")
                .message()
                .body("Message content to send")
        );

        run(receive("mqConsumer")
                .message()
                .body("Expected message content"));
    }
}


The Kafka infrastructure (“embeddedKafka” bean) is automatically started with the test. So the Citrus Kafka producer endpoint just needs to connect to the Kafka server broker. The test sends a message to a queue and then receives a message from another queue and checks the result.

Of course don't forget to configure your SUT to interact with your “embeddedKafka”.


That's all for now. I wish you successful application of Citrus framework solutions in your test projects!)