testcontainers-kafka-test

Utilities for kafka integration tests

License

License

Categories

Categories

Container
GroupId

GroupId

com.github.ydespreaux.testcontainers.kafka.test
ArtifactId

ArtifactId

testcontainers-kafka-test
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

testcontainers-kafka-test
Utilities for kafka integration tests
Project URL

Project URL

https://github.com/ydespreaux/testcontainers-kafka-test
Source Code Management

Source Code Management

https://github.com/ydespreaux/testcontainers-kafka-test

Download testcontainers-kafka-test

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ydespreaux.testcontainers.kafka.test/testcontainers-kafka-test/ -->
<dependency>
    <groupId>com.github.ydespreaux.testcontainers.kafka.test</groupId>
    <artifactId>testcontainers-kafka-test</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ydespreaux.testcontainers.kafka.test/testcontainers-kafka-test/
implementation 'com.github.ydespreaux.testcontainers.kafka.test:testcontainers-kafka-test:1.0.0'
// https://jarcasting.com/artifacts/com.github.ydespreaux.testcontainers.kafka.test/testcontainers-kafka-test/
implementation ("com.github.ydespreaux.testcontainers.kafka.test:testcontainers-kafka-test:1.0.0")
'com.github.ydespreaux.testcontainers.kafka.test:testcontainers-kafka-test:jar:1.0.0'
<dependency org="com.github.ydespreaux.testcontainers.kafka.test" name="testcontainers-kafka-test" rev="1.0.0">
  <artifact name="testcontainers-kafka-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ydespreaux.testcontainers.kafka.test', module='testcontainers-kafka-test', version='1.0.0')
)
libraryDependencies += "com.github.ydespreaux.testcontainers.kafka.test" % "testcontainers-kafka-test" % "1.0.0"
[com.github.ydespreaux.testcontainers.kafka.test/testcontainers-kafka-test "1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.kafka : spring-kafka jar 1.1.2.RELEASE
org.springframework.kafka : spring-kafka-test jar 1.1.2.RELEASE
com.github.ydespreaux.testcontainers : testcontainers-kafka jar 1.0.0

provided (1)

Group / Artifact Type Version
io.confluent » kafka-avro-serializer jar 4.1.0

test (3)

Group / Artifact Type Version
org.apache.avro : avro jar 1.8.2
org.springframework.boot : spring-boot-starter-test jar 1.5.0.RELEASE
org.springframework.boot : spring-boot-starter jar 1.5.0.RELEASE

Project Modules

There are no modules declared in this project.

testcontainers-kafka-test

Create KafkaTemplate, KafkaMessageContainerListener for integration tests with kafka.

Utility library for Kafka integration tests. The Kafka containerized is detailed at the following url: https://github.com/ydespreaux/testcontainers

Versions

lib-testcontainers-kafka-utils Spring Boot testcontainers-kafka
1.2.0 2.1.x 1.0.x
1.1.0 2.0.x 1.0.x
1.0.0 1.5.x 1.0.x

Add the Maven dependency

<dependency>
    <groupId>com.github.ydespreaux.testcontainers</groupId>
    <artifactId>testcontainers-kafka-test</artifactId>
    <version>1.2.0</version>
    <scope>test</scope>
</dependency>

AvroSerializerFactory

This factory makes it possible to create AVRO serializer / deserializer.

/**
 * Create a avro serializer.
 *
 * @param isKey set if the serializer is applied to the key or message value
 * @return
 */
public KafkaAvroSerializer createKafkaAvroSerializer(Boolean isKey);
/**
 * Create a avro deserializer.
 * @param isKey set if the serializer is applied to the key or message value
 * @return
 */
public KafkaAvroDeserializer createKafkaAvroDeserializer(Boolean isKey);
/**
 * Create a avro deserializer.
 * @param isKey set if the serializer is applied to the key or message value
 * @param specificAvroReader If true, tries to look up the SpecificRecord class
 * @return
 */
public KafkaAvroDeserializer createKafkaAvroDeserializer(Boolean isKey, Boolean specificAvroReader);

Example:

@ClassRule
public static final ConfluentKafkaContainer kafkaContainerWithSchemaRegistry = new ConfluentKafkaContainer()
        .withSchemaRegistry(true)

KafkaAvroSerializer keySerializer = new AvroSerializerFactory(kafkaContainerWithSchemaRegistry).createKafkaAvroSerializer(true);
KafkaAvroSerializer valueSerializer = new AvroSerializerFactory(kafkaContainerWithSchemaRegistry).createKafkaAvroSerializer(false);

KafkaTemplateFactory

This factory makes it possible to create KafkaTemplates.

/**
 * Create a kafkaTemplate with additional properties.
 */
public <K, V> KafkaTemplate<K, V> createKafkaTemplate(Map<String, Object> additionalProperties);

/**
 * Create a kafkatemplate with specific key serializer and value serializer.
 */
public <K, V> KafkaTemplate<K, V> createKafkaTemplate(String keySerializerClass, String valueSerializerClass);

/**
 * Create a kafkatemplate with optional properties and a specific key serializer and value serializer.
 */
public <K, V> KafkaTemplate<K, V> createKafkaTemplate(Map<String, Object> additionalProperties, String keySerializerClass, String valueSerializerClass);

/**
 * Create a kafkatemplate with specific key serializer  and value serializer.
 */
public <K, V> KafkaTemplate<K, V> createKafkaTemplate(Serializer<K> keySerializer, Serializer<V> valueSerializer);

/**
 * Create a kafkatemplate with optional properties and specific key serializer  and value serializer
 */
public <K, V> KafkaTemplate<K, V> createKafkaTemplate(Map<String, Object> additionalProperties, Serializer<K> keySerializer, Serializer<V> valueSerializer);

Example

@ClassRule
public static final ConfluentKafkaContainer kafkaContainer = new ConfluentKafkaContainer();

KafkaTemplate<String, String> template =
        new KafkaTemplateFactory(kafkaContainer)
                .createKafkaTemplate("org.apache.kafka.common.serialization.StringSerializer", "org.apache.kafka.common.serialization.StringSerializer");

Format Avro:

@ClassRule
public static final ConfluentKafkaContainer kafkaContainerWithSchemaRegistry = new ConfluentKafkaContainer()
        .withSchemaRegistry(true);
KafkaTemplate<String, Object> template =
        new KafkaTemplateFactory(kafkaContainerWithSchemaRegistry)
            .createKafkaTemplate(
                new StringSerializer(),
                new AvroSerializerFactory(kafkaContainerWithSchemaRegistry).createKafkaAvroSerializer(false));

KafkaMessageListenerContainerFactory

This factory makes it possible to create listening containers.

public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        String topic,
        int partitions,
        MessageListener<?, ?> listener) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        String topic,
        int partitions,
        MessageListener<?, ?> listener,
        Map<String, Object> optionalProperties) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        String[] topics,
        int partitions,
        MessageListener<?, ?> listener) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        String[] topics,
        int partitions,
        MessageListener<?, ?> listener,
        Map<String, Object> optionalProperties) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        Pattern topicPattern,
        int partitions,
        MessageListener<?, ?> listener) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(
        Deserializer<K> keyDeserializer,
        Deserializer<V> valueDeserializer,
        String group,
        Pattern topicPattern,
        int partitions,
        MessageListener<?, ?> listener,
        Map<String, Object> optionalProperties) throws Exception;
public <K, V> KafkaMessageListenerContainer<K, V> createListenerContainer(Deserializer<K> keyDeserializer,
                                                                           Deserializer<V> valueDeserializer,
                                                                           String group,
                                                                           int partitions,
                                                                           MessageListener<?, ?> listener,
                                                                           Map<String, Object> optionalProperties,
                                                                           ContainerProperties containerProperties) throws Exception;

Example:

@ClassRule
public static final ConfluentKafkaContainer kafkaContainer = new ConfluentKafkaContainer();

BlockingQueue<ConsumerRecord<String, String>> records = new LinkedBlockingQueue<>();
KafkaMessageListenerContainer<String, String> listenerContainer = new KafkaMessageListenerContainerFactory(kafkaContainer)
        .createListenerContainer(
                new StringDeserializer(),
                new StringDeserializer(),
                "an_group",
                "my_topic",
                1,
                (MessageListener<String, String>) record -> records.add(record));

AVRO message:

@ClassRule
public static final ConfluentKafkaContainer kafkaContainerWithSchemaRegistry = new ConfluentKafkaContainer()
        .withSchemaRegistry(true)

BlockingQueue<ConsumerRecord<String, WorkstationAvro>> records = new LinkedBlockingQueue<>();
KafkaMessageListenerContainer<String, Object> listenerContainer = new KafkaMessageListenerContainerFactory(kafkaContainerWithSchemaRegistry)
        .createListenerContainer(
                new StringDeserializer(),
                new AvroSerializerFactory(kafkaContainerWithSchemaRegistry).createKafkaAvroDeserializer(false, true),
                "an_group",
                "my_topic_avro",
                1,
                (MessageListener<String, WorkstationAvro>) record -> records.add(record));

Versions

Version
1.0.0