Skip to content

wojtek-szymanski/kafka-junit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kafka JUnit Rule Build Status Maven Central

JUnit rule for starting and tearing down a Kafka broker during tests.

Version Kafka Version
1.6 0.8.2.1
1.7 0.8.2.2
1.8 0.9.0.0
2.0 0.9.0.1

Please note that version 2.0 is for Java 1.8 and up and contains some breaking API changes.

Installation

Releases are available on Maven Central.

Maven Central

Snapshot versions containing builds from the latest master are available in the Sonatype snapshots repo.

Usage

Create an instance of the rule in your test class and annotate it with @Rule. This will start and stop the broker between each test invocation.

@Rule
public KafkaJunitRule kafkaRule = new KafkaJunitRule();

To spin up the broker at the beginning of a test suite and tear it down at the end, use @ClassRule.

@ClassRule
public static KafkaJunitRule kafkaRule = new KafkaJunitRule();

kafkaRule can be referenced from within your test methods to obtain information about the Kafka broker.

@Test
public void testSomething(){
    // Use the built-in producer
    KafkaProducer<String, String> producer = kafkaRule.createStringProducer();

    // Use the built-in consumer 
    KafkaConsumer<String, String> consumer = kafkaRule.createStringConsumer();

    // Alternatively, the Zookeeper connection String and the broker port can be retrieved to generate your own config
    String zkConnStr = kafkaRule.zookeeperConnectionString();
    int brokerPort = kafkaRule.kafkaBrokerPort();

    ...
}

There are also helper methods available to read a number of messages with a configurable timeout.

@Test
public void testMessagesCanBeRead() throws TimeoutException {
    // write a message 
    try (KafkaProducer<String, String> producer = kafkaRule.createStringProducer()) {
        producer.send(new ProducerRecord<>(TOPIC, KEY, VALUE));
    }

    // attempt to read a single message with a 5 second timeout
    List<String> messages = kafkaRule.readStringMessages(TOPIC, 1, 5);
    assertThat(messages, is(notNullValue()));
    assertThat(messages.size(), is(1));

    String msg = messages.get(0);
    assertThat(msg, is(notNullValue()));
    assertThat(msg, is(equalTo(VALUE)));
}

Refer to unit tests for more examples.

About

JUnit rule for spinning up a Kafka broker

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Java 100.0%