Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/5416fcf6272cd91e91ffb462ff9a47ba to your computer and use it in GitHub Desktop.
Save dacr/5416fcf6272cd91e91ffb462ff9a47ba to your computer and use it in GitHub Desktop.
Hello world embedded kafka test script using scalatest-embedded-kafka. / published by https://github.com/dacr/code-examples-manager #671230f8-20da-456c-ad5a-e2bbb0dc7377/b810485bc0886d28282d07cbf5487b6ddebffe1c
// summary : Hello world embedded kafka test script using scalatest-embedded-kafka.
// keywords : scala, scalatest, kafka, embedded-kafka
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 671230f8-20da-456c-ad5a-e2bbb0dc7377
// created-on : 2018-10-10T08:45:12+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : cs launch --scala 2.13 com.lihaoyi:::ammonite:2.4.0 -M ammonite.Main -- $file
import $ivy.`org.scalatest::scalatest:3.2.9`
import $ivy.`io.github.embeddedkafka::embedded-kafka:2.8.0`
import $ivy.`org.apache.logging.log4j:log4j-api:2.14.1`
import $ivy.`org.apache.logging.log4j:log4j-core:2.14.1`
import $ivy.`org.apache.logging.log4j:log4j-slf4j-impl:2.14.1`
import org.scalatest._, wordspec._, matchers._, OptionValues._
import io.github.embeddedkafka._
import org.apache.kafka.common.serialization.{StringSerializer, StringDeserializer}
org.apache.logging.log4j.core.config.Configurator.setRootLevel(org.apache.logging.log4j.Level.ERROR)
class KafkaHelloTest extends AnyWordSpec with should.Matchers with EmbeddedKafka {
override val suiteName = "KafkaHelloTest"
"Kafka embedded" should {
"work to send and receive a message within a test" in
withRunningKafka {
implicit val serializer = new StringSerializer
implicit val deserializer = new StringDeserializer
publishToKafka("news", "Hello world !")
consumeFirstMessageFrom("news") should include regex "(?i)hello"
}
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[KafkaHelloTest].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment