Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear topics in tests with Spring Kafka

I'm writing a unit test with Spring Kafka 2.4 to prove that my Spring Boot setup is correct. I'm validating that SeekToCurrentBatchErrorHandler works as expected which requires sending an incorrect message that should be retried. Unfortunately this incorrect messages breaks other tests because this message will be retried forever.

Because of above I'd like to ensure that each test is correctly isolated. I either need to:

  1. Delete and recreate the Kafka topic with AdminClient

  2. Seek to the end of the existing Kafka topic and commit new offsets

I was trying option 2 with Consumer.seekToEnd() method however Spring Kafka hides the created consumers behind few layers of internal framework classes. I'm also not 100% sure if this method can be called in test thread which is different from listener thread.

What is the recommended way to clear topics in tests with Spring Kafka?

like image 803
Karol Dowbecki Avatar asked Oct 28 '25 05:10

Karol Dowbecki


1 Answers

Best practice is to use unique topic names in each test to provide complete isolation; you could also stop the container(s), create a new Consumer with the same group.id and perform the seeks there.

like image 94
Gary Russell Avatar answered Oct 30 '25 22:10

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!