2

I've tried to test my code locally by setting up a Kafka server and sending messages using a producer, but I am wondering if there is a way I can write a unit test for this piece of code (test whether the message received by the consumer is correct).

val consumerSettings = ConsumerSettings(system, 
  new ByteArrayDeserializer, new StringDeserializer)
  .withBootstrapServers("localhost:9092")
  .withGroupId("group1")
  .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")

val done = Consumer.committableSource(consumerSettings, 
  Subscriptions.topics("topic1"))
  .map { msg =>
    msg.committableOffset.commitScaladsl()
  }
  .runWith(Sink.ignore)
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
Isa
  • 353
  • 2
  • 18

1 Answers1

1

You could test your code with the following tools:

  • ScalaTest
  • scalatest-embedded-kafka: enables the creation of an in-memory Kafka instance that works with ScalaTest.
  • Akka Streams Testkit: provides a TestSink.probe that can inspect, and control the demand of, stream elements (i.e., messages).

The Akka Streams Kafka project uses the above in its own tests. Take a look at its IntegrationSpec, which you can adapt for your needs.

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54