Simulating Producers

Producing events is one part of message exchange in Kafka, the other being consuming events. You can use ReadyAPI to simulate an event producer and post events to topics on your Kafka broker.

Use case

Let's consider an example in which you want to verify that your payment system works correctly. The system reads events from the payments topic and, if the payment is successful, publishes events to the payment-confirmation topic.

So, to make sure the system works, you can use ReadyAPI to simulate a producer and publish payment events.

Tip

If you want to simulate a complex scenario, you would usually also want to simulate an event consumer to check how the event is processed. In our example, we could use ReadyAPI to verify that the payment system published a message in the payment-confirmation topic.

Produce events

1. Add a Kafka API to the project

To start testing Kafka APIs, you should first add one to the project. To learn how to do it, see Adding a Kafka API to the Project.

2. Create a test step

To produce a Kafka event in ReadyAPI, you use the API Connection test step based on a Publish operation:

  1. Open a test case and add API Connection Test Step.

  2. In the dialog that appears, select a Publish operation and click Add Test Step.

    Note

    In AsyncAPI 3.0, the Publish operation is replaced with Send. The workflow remains the same regardless of the AsyncAPI version you use.

    Example 7. Adding API Connection Test Step.
    ReadyAPI Add API Connection Test Step dialog showing Kafka API selected in the Choose API panel with quickstart-events channel and Publish operation highlighted, and Kafka MCP Case selected in the Choose Test Case panel


Alternatively:

  1. Right-click a Publish operation in the APIs node in the Navigator and select Add to Test Case.

  2. In the Add API Connection Test Step dialog, select a test case and click Add Test Step.

    Example 8. Adding the Publish operation.
    Simulating Kafka producer: Add Publish operation to test step


3. Send the event

To publish the event to the topic, enter the message in the Data field and select Connect. ReadyAPI will send the event and then disconnect.

ReadyAPI Test Step API Connection showing KAFKA protocol, localhost:9092 endpoint, PUBLISH operation on quickstart-events channel, Connect button highlighted, and Sent Data section with JSON data body containing test and value fields

In the sections below, you can read more about connection settings and the message editor. Kafka authentication settings are covered here: Authentication in Kafka.

Test step settings

Test step toolbar

The test step toolbar contains settings that allow you to modify the connection and authorization settings for the test step:

ReadyAPI Test Step API Connection showing KAFKA protocol, localhost:9092 endpoint, Inherit From Parent authorization profile, PUBLISH operation, quickstart-events channel, and Connect and Connection Settings buttons

Setting

Description

Protocol

The protocol used by the test step. For Kafka test steps, this is always KAFKA.

Endpoint

The endpoint of the Kafka broker.

Authorization Profile

The authorization profile used to authenticate with the broker.

Operation

The operation the test step is linked to - Publish or Subscribe (or Send / Receive in AsyncAPI 3.0). To change it, click Link API.

Channel

The topic on the broker to which the event is sent or from which it is received.

Connect

Sends the event to the topic.

Connection Settings

Opens the connection settings dialog.

Connection settings

To open the connection settings, select Connection Settings in the test step toolbar.

Option

Description

Confluent Settings

Used to connect to the Confluent schema registry. Only available when the JSON via Avro (Schema Registry) or JSON via Protobuf (Schema Registry) message format is selected.

  • Schema Registry URL – The URL of the schema registry.

  • Registry Authorization – The authorization profile that will be used to authenticate to the schema registry. To learn more about registry schema authentication, see Authentication in Kafka.

Publish Settings

Other parameters that your Kafka provider might require.

Reset to Default

Resets connection settings to the default. Default settings depend on the environment selected:

  • If no environment is selected, Confluent Settings and Publish Settings are cleared.

  • If an environment is selected, the default settings will be taken from that environment's settings.

Property list

You can also change the test step behavior by using the step properties on the API Connection Test Step Properties panel in the Navigator.

Property

Description

Name

The test step’s name.

Description

Text that describes the test step.

Message editor

The message editor is where you enter the actual message that the test step will send to the topic.

ReadyAPI Test Step API Connection showing KAFKA protocol, localhost:9092 endpoint, PUBLISH operation on quickstart-events channel, with Sent Data section highlighted showing two metadata entries: client-id of type Header with value idxxxxxxxx, and Partiti
  • Message format – The format in which the message will be sent. Currently, two formats are available:

    • JSON – The message will be sent as a regular JSON.

    • JSON via Avro (Schema Registry) – The message will be serialized using an Avro schema stored in the Confluent schema registry. You can set up a connection to the schema registry in the Connection Settings.

    • JSON via Protobuf (Schema Registry) – The message will be serialized using a Protobuf schema stored in the Confluent schema registry. You can set up a connection to the schema registry in the Connection Settings.

    • Custom – The message will be serialized using an Avro or Protobuf schema stored in a file. You can set up a connection to the schema registry in the Connection Settings.

      Note

      When testing Kafka in ReadyAPI, ReadyAPI allows for fetching Avro/Protobuf key schemas from a file.

  • Metadata – The parameters that will be passed with the message. To add a parameter, click add-property.png.

    There are three types of parameters:

    • Header – Kafka headers sent with the message.

    • Path – Used to update path parameter values in the channel name.

      ReadyAPI Test Step API Connection showing KAFKA protocol, localhost:9092 endpoint, PUBLISH operation with topic/{param} channel containing a path parameter named param of type Path with value new-topic, and JSON data body with param and value fields
    • Kafka – Other parameters specific to Kafka. Currently, two Kafka parameters are supported: Key and Partition.

    Tip

    You can use property expansions to insert data into parameter values.

  • Data – The message itself in the JSON format.

    You can insert data from other test steps or parameters into the message by using Get Data. To learn more about the feature, see Get Data Dialog.

    You can also automatically format the JSON you enter in the field by clicking the Beautify button.

Next Steps

See Also

Publication date: