The Kroxylicious project is very pleased to announce the 0.7.0 release of our Junit5 Extension which adds the ability for test authors to automatically inject topics into their tests.
The Junit5 extension aims to simplify writing tests against Kafka clusters by providing test authors with ability to decoratively control a Kafka cluster to test against without worrying about the details of provisioning the cluster.
import java.time.Duration;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import io.kroxylicious.testing.kafka.api.KafkaCluster;
import io.kroxylicious.testing.kafka.common.BrokerCluster;
import io.kroxylicious.testing.kafka.junit5ext.KafkaClusterExtension;
import io.kroxylicious.testing.kafka.junit5ext.Topic;
@ExtendWith(KafkaClusterExtension.class)
class ExampleTest {
@BrokerCluster
KafkaCluster cluster;
@Test
void shouldConsumePublishedRecord(Topic topic, Producer<String, String> producer, Consumer<String, String> consumer)
throws ExecutionException, InterruptedException {
//Given
producer.send(new ProducerRecord<>(topic.name(), "my-key", "Hello, world!")).get();
consumer.subscribe(Set.of(topic.name()));
//When
var records = consumer.poll(Duration.ofSeconds(10));
//Then
consumer.close();
Assertions.assertThat(records).hasSize(1).extractingResultOf("value").containsExactly("Hello, world!");
}
}
As part of this release users now need to ensure they have org.apache.kafka:kafka_2.12
or org.apache.kafka:kafka_2.13
available in test scope. This change allows users to change Kafka versions without waiting for a new release of the extension to support that Kafka version.
Further details about controlling the Kafka Cluster such as enabling KRaft support are can be found in the projects readme.
Please let us know, through Slack or GitHub, if you find the extension interesting or helpful