Kafka Streams testing tip: Making sense of "Uninitialized topic" exception

abhirockzz profile image Abhishek Gupta ・2 min read

One of my previous blog post discussed testing Kafka Streams application.
Here is a related tip that might come in handy!

While testing, you might come across java.util.NoSuchElementException: Uninitialized topic exception. Most probably, this is due to the fact that you invoked TestOutputTopic.readValue (e.g. as part of an assert check). The problem is not the exception itself, but the error message Uninitialized topic which can be confusing. The exception is likely to pop up if your processing Topology did not work as expected and there was no record in the topic (TestOutputTopic) you were trying to read from.

Consider this example:

TestInputTopic<String, String> inputTopic = ...;
TestOutputTopic<String, String> outputTopic = ...;

inputTopic.pipeInput("foo", "bar");
assertThat(outputTopic.readValue(), equalTo("foobar"));

To avoid confusion, you should confirm whether the output topic is empty - both before and (optionally) after checking for the value, e.g.

assertThat("output topic was empty",outputTopic.isEmpty(), is(false));
inputTopic.pipeInput("foo", "bar");
assertThat(outputTopic.readValue(), equalTo("BAR"));
assertThat("output topic was not empty",outputTopic.isEmpty(), is(true));

Having the assertThat (with a proper error message) before asserting outputTopic.readValue() ensures that you get a failure response which you can make sense of. Something that looks like:

testCase1(foo.bar.AppTest)  Time elapsed: 0.066 sec  <<< FAILURE!
java.lang.AssertionError: output topic was empty
Expected: is <false>
     but: was <true>
    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
    at org.junit.Assert.assertThat(Assert.java:956)
    at com.foo.bar.AppTest.testCase1(AppTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Now output topic was empty make sense.. at least to me!

If you're interested in Kafka Streams, do check out some of my previous blogs! Happy to get your feedback via Twitter or just drop a comment 🙏🏻

Posted on Mar 18 by:

abhirockzz profile

Abhishek Gupta


Currently working with Kafka, Databases, Azure, Kubernetes and related open source projects


ITNEXT is a platform for software developers, engineers, IT architects, system engineers and IT enthousiasts to share knowledge, connect and connect.


markdown guide