Skip to content

Introduce @KafkaListenerTest for focused Kafka Listener testing #45706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gustavomonarin opened this issue May 28, 2025 · 1 comment
Open
Labels
for: team-meeting An issue we'd like to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged

Comments

@gustavomonarin
Copy link

gustavomonarin commented May 28, 2025

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 28, 2025
@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label May 28, 2025
@LeMikaelF
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-meeting An issue we'd like to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants