Kotest offers an extension that spins up an embedded Kafka instance. This can help in situations where using the kafka docker images are an issue.
To use this extension add the
io.kotest.extensions:kotest-extensions-embedded-kafka module to your test compile path.
embeddedKafkaListener listener in your test class:
And the broker will be started once the spec is created and stopped once the spec completes.
Note: The underlying embedded kafka library uses a global object for state. Do not start multiple kafka instances at the same time.
To create a consumer and producer we can use convenience methods on the listener:
stringStringConsumer methods return a producer / consumer that accept strings for the keys and values. Similar methods exist for byte pairs.
Alternatively, you can access the host/port the Kafka instance was deployed on and create the clients yourself:
You can create a new instance of the listener specifying a port and then use that instance rather than the default instance.
You can also do specify the zookeeper port using an alternative overload.