beginnerTesting the Producer
Why is testing against an embedded/in-memory Kafka broker preferred over mocking the `KafkaTemplate` entirely?
Mocking `KafkaTemplate` only proves your code CALLED send() with the right arguments — it says nothing about serialization actually working, whether your message satisfies the topic's schema, or whether the producer config itself is valid. An embedded broker (or Testcontainers Kafka) runs real serialization and a real (if temporary) broker, catching a whole class of bugs mocks can't see.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response