• Stars
    star
    174
  • Rank 219,104 (Top 5 %)
  • Language
    Java
  • License
    Other
  • Created almost 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Kafka Unit Testing

TravisCI

Allows you to start and stop a Kafka broker + ZooKeeper instance for unit testing applications that communicate with Kafka.

Versions

kafka-unit Kafka broker Zookeeper
1.0 kafka_2.11:0.11.0.0 3.4.10
0.7 kafka_2.11:0.10.0.2 3.4.10
0.6 kafka_2.11:0.10.0.0 3.4.6
0.5 kafka_2.11:0.9.0.1 3.4.6
0.4 kafka_2.11:0.9.0.1 3.4.6
0.3 kafka_2.11:0.8.2.2 3.4.6
0.2 kafka_2.11:0.8.2.1 3.4.6

Maven central

<dependency>
    <groupId>info.batey.kafka</groupId>
    <artifactId>kafka-unit</artifactId>
    <version>1.0</version>
</dependency>

Starting manually

To start both a Kafka server and ZooKeeper instance on random ports use following code:

KafkaUnit kafkaUnitServer = new KafkaUnit();
kafkaUnitServer.startup();
kafkaUnitServer.shutdown();

ZooKeeper and Kafka broker ports can be specified explicitly using second constructor, which takes two ints:

KafkaUnit kafkaUnitServer = new KafkaUnit(5000, 5001);

The alternative constructor allows providing connection strings rather than ports, which might be convenient if you want to use existing config without parsing it to extract port numbers:

KafkaUnit kafkaUnitServer = new KafkaUnit("localhost:5000", "localhost:5001");

Currently only localhost is supported and it's required that the connection string consists of only one localhost:[port] pair.

You can then write your own code to interact with Kafka or use the following methods:

kafkaUnitServer.createTopic(testTopic);
ProducerRecord<String, String> keyedMessage = new ProducerRecord<>(testTopic, "key", "value");
kafkaUnitServer.sendMessages(keyedMessage);

And to read messages:

List<String> messages = kafkaUnitServer.readMessages(testTopic, 1);
List<String> allMessages = kafkaUnitServer.readAllMessages(testTopic);

Only String messages are supported at the moment.

Alternatively, you can use getKafkaConnect() to manually configure producer and consumer clients like:

Properties props = new Properties();
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getCanonicalName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getCanonicalName());
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaUnitServer.getKafkaConnect());

Producer<Long, String> producer = new KafkaProducer<>(props);

Using the JUnit Rule

If you don't want to start/stop the server manually, you can use the JUnit rule, e.g.

public class KafkaUnitIntegrationTest {

    @Rule
    public KafkaUnitRule kafkaUnitRule = new KafkaUnitRule();

    @Test
    public void junitRuleShouldHaveStartedKafka() throws Exception {
        String testTopic = "TestTopic";
        kafkaUnitRule.getKafkaUnit().createTopic(testTopic);
        ProducerRecord<String, String> keyedMessage = new ProducerRecord<>(testTopic, "key", "value");

        kafkaUnitServer.sendMessages(keyedMessage);
        List<String> messages = kafkaUnitRule.getKafkaUnit().readMessages(testTopic, 1);

        assertEquals(Arrays.asList("value"), messages);
    }
}

This will start/stop the broker every test, so that particular test can't interfere with the next. Contrary to KafkaUnit() constructor, it does not throw checked IOException when socket initialization fails, but wraps it in runtime exception and thus is suitable for use as @Rule field in tests.

If you want to start server on specific ports, use KafkaUnitRule(int, int) or KafkaUnitRule(String, String) constructor, which accepts ZooKeeper and Kafka broker ports or connection strings respectively (just like corresponding KafkaUnit constructors), e.g.:

    @Rule
    public KafkaUnitRule kafkaUnitRule = new KafkaUnitRule(5000, 5001);

License

Copyright 2013 Christopher Batey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

docker-jvm-talk

Resources for my talk on running JVMs inside Docker
Java
28
star
2

vagrant-cassandra-spark

Shell
27
star
3

killrauction

Java
25
star
4

java-async-talk

Resources for a conference talk on async tools for Java
Java
17
star
5

vagrant-wiremock-saboteur

Java
16
star
6

scala-basics-talk

Scala
13
star
7

dropwizard-hystrix

Java
12
star
8

akka-talks

Scala
11
star
9

integration-points-example

Using tenacity + breakerbox with dropwizard to monitor integration points
Java
9
star
10

ubuntu-ansible

Shell
9
star
11

akka-http-typed

Scala
7
star
12

docker-jvm-akka

Java
7
star
13

docker-jvm-flamegraphs

Example project for showing how to use Linux perf for JVMs inside Docker containers
Java
7
star
14

cassandra-health-check

Java
6
star
15

scala-testing

Examples of how to test Scala classes
Scala
4
star
16

cassandra-rest-server

Using the JSON support to provide a test interface to Cassandra
Java
4
star
17

scassandra-example-java

Java
4
star
18

six-things-in-akka-2.6

Demo project for "Six things in Akka 2.6"
Scala
3
star
19

cassandra-anti-patterns

Java
3
star
20

spark-sandbox

Scala
3
star
21

java9-gc-log-parser

Scala
2
star
22

akka-typed-talk

HTML
2
star
23

cassandra-lwts-summit

Java
2
star
24

cassandra-examples

Experiments with Cassandra 2.2
2
star
25

spring-cloud-example

Java
2
star
26

ansible-home-office

ansible-home-office
Shell
2
star
27

cassandra-customer-events-dropwizard

Java
2
star
28

linux-home-office

Shell
2
star
29

dot-files

Vim Script
2
star
30

cassandra-killr

Java
2
star
31

akka-projection-end-to-end

Scala
2
star
32

akka-streams-cassandra

Scala
2
star
33

memcache-netty

Java
1
star
34

cassandra-customer-events

Example project using the DataStax Java Driver
Java
1
star
35

akka-grpc-kubernetes

Example showing Akka HTTP -> Akka gRPC inside of Kubernetes using Akka Discovery
Scala
1
star
36

GroovyTestingJava

Basic Gradle project with Java production code and Groovy tests
Groovy
1
star
37

vagrant-logstash

1
star
38

mockito-iterator

A way to mock iterable objects with mockito
Java
1
star
39

akka-cluster-kubernetes

Scala
1
star
40

strata-cassandra-workshop

Java
1
star
41

scassandra-cucumber-example

Java
1
star
42

concurrency-sandbox

Java
1
star
43

akka-persistence-datastore

Scala
1
star
44

haskell-study-group

Haskell
1
star