Currently, testing Kafka listeners typically involves @SpringBootTest and @EmbeddedKafka, which loads the entire application context (as documented here).

To enable more lightweight and focused testing of Kafka listeners, similar to how @WebMvcTest allows for testing web controllers in isolation, could anew new test slice be introduced? @KafkaListenerTest.

This slice would focus on testing a specific listener and its associated configuration (e.g., serialization/deserialization, error handlers) without the overhead of the full application context.

This could significantly improve the efficiency and speed of testing Kafka listener components.

Any thoughts on this proposal?

Comment From: LeMikaelF

I've found that a combination of @SpringBootTest(classes = MyListener.class) (it's important to specify classes explicitly so that it won't load the whole context), @ImportAutoConfiguration(KafkaAutoConfiguration.class), and @EmbeddedKafka is fairly lightweight. This would take care of your "slicing" needs.

For something even lighter, a while back I made an annotation @KafkaMockConsumerTest that worked similarly to @WebMvcTest (using a MockConsumer instead of the actual broker). See the annotation and an example test. I never ended up actually using it, because the @EmbeddedKafka solution is fast enough for me, and because I didn't feel it was reliable enough as a homegrown solution, but it could be different if it was supported by Spring Boot.