I have a simple Spring Boot application which reads from Kafka and writes to Kafka. I wrote a SpringBootTest
using an EmbeddedKafka
to test all that.
The main problem is: Sometimes the test fails because the test sends the Kafka message too early. That way, the message is already written to Kafka before the Spring application (or its KafkaListener
to be precise) is ready. Since the listener reads from the latest
offset (I do not want to change any config for my test - except bootstrap.servers), it will not receive all messages in that test.
Does anyone have an idea how I could know inside the test, that the KafkaListener
is ready to receive messages?
Only way I could think of is waiting until /health
comes available but I have no idea whether I can be sure that this implies the KafkaListener
to be ready at all.
Any help is greatly appreciated!
Best regards.
If you have a KafkaMessageListenerContainer
instance, then it is very easy to use org.springframework.kafka.test.utils.ContainerTestUtils.waitForAssignment(Object container, int partitions)
.
https://docs.spring.io/spring-kafka/api/org/springframework/kafka/test/utils/ContainerTestUtils.html
e.g. calling ContainerTestUtils.waitForAssignment(container, 1);
in your Test setup will block until the container has gotten 1 partition assigned.