Version 2.7.0 of Kafka for JUnit has been released. It increases all Kafka dependencies to 2.7.0 and fixes a couple of minor SonarLint findings. It also improves API design when it comes to the configuration of the cluster as well as producing and consuming records. In prior versions you had to explicitly conclude the available builders by calling build() or useDefaults() if the default parameterization is acceptable to our test case. With 2.7.0 we introduced convenience methods into the API that do this for you. This makes writing tests a little less noisy, as superfluous method calls are no longer necessary. A simple test case that launches an embedded Kafka cluster with a single broker and default settings, produces a couple of records and observes that they actually reach the designated topic looks like this:

class RecordProducerTest {

    private EmbeddedKafkaCluster kafka;

    @BeforeEach
    void prepareEnvironment() {
        kafka = provisionWith(defaultClusterConfig());
        kafka.start();
    }

    @AfterEach
    void tearDownEnvironment() {
        if (kafka != null) kafka.stop();
    }

    @Test
    void sendingUnkeyedRecordsWithDefaults() throws Exception {

        kafka.send(to("test-topic", "a", "b", "c"));

        assertThat(kafka.observeValues(on("test-topic", 3)).size())
                .isEqualTo(3);
    }
}

There is no immediate migration required. It is still possible to write tests like you did before, although it is recommended to stick to the new approach. A couple of methods got marked as @Deprecated (the Javadoc gives details on the migration path), which means that they will be removed in a future release. All listings of the user's guide have been updated to demonstrate the use of the new convenience methods.

Changelog

See the changelog on GitHub.

Features

Bugfixes

None.

Get it

You can obtain the binaries from Maven central or include the dependency using the following Maven coordinates in your build.

Maven

<dependency>
  <groupId>net.mguenther.kafka</groupId>
  <artifactId>kafka-junit</artifactId>
  <version>2.7.0</version>
</dependency>

Gradle

compile 'net.mguenther.kafka:kafka-junit:2.7.0'

Hi there! I'm Markus!

I'm an independent freelance IT consultant, a well-known expert for Apache Kafka and Apache Solr, software architect (iSAQB certified) and trainer.

How can I support you?

GET IN TOUCH