Duration: 45 mins
Add test dependencies to build.sbt:
libraryDependencies += "org.testcontainers" % "kafka" % "1.20.4" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
ScalaTest spec that:
org.testcontainers.containers.KafkaContainer in
beforeAll (use a @Container-style lifecycle or plain
start()/stop() in beforeAll/afterAll)getBootstrapServers and uses it to build
producer/consumer Properties — the whole point is your app code
doesn’t need to know it’s talking to a test containerWrite one test: produce 3 Order records (reuse the (de)serializers
from 08_custom_serialization.md) with
OrderProducerApp’s logic, then consume them back with
OrderConsumerApp’s logic and assert you get exactly those 3
records back, in the order sent (single partition, single producer —
ordering is guaranteed here)
Write a second test asserting key-based ordering under repartitioning: send several records under the same key, consume, and assert they arrive in send order — same guarantee, framed as a test of the property you actually depend on in production
Run the suite. Confirm each test starts a fresh, isolated broker (no leftover topics/data bleeding between test runs) — that’s the main reason to reach for Testcontainers over a shared dev broker in CI