Git Product home page Git Product logo

kafka-unit's Introduction

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
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>0.7</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);
KeyedMessage<String, String> keyedMessage = new KeyedMessage<>(testTopic, "key", "value");
kafkaUnitServer.sendMessages(keyedMessage);

And to read messages:

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

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);
        KeyedMessage<String, String> keyedMessage = new KeyedMessage<>(testTopic, "key", "value");

        kafkaUnitRule.getKafkaUnit().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.

kafka-unit's People

Contributors

chbatey avatar soid avatar edwardmlyte avatar xaerxess avatar felipefzdz avatar reda-alaoui avatar choang avatar cwensel avatar craigwilliams84 avatar franekrichardson avatar jvrmaia avatar regispl avatar sakanaou avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.