0

I'm using Kafka embedded broker with spring boot and junit 5.I have been able to wire up successfully and see that the embedded broker is running.

In my setup method I pump in a few messages to the queue that my actual code listens on

    @BeforeAll
    public void setup() {
             // code to play down some messages to topic X
    }

My consumer/listener is never trigerred despite there being no errors encountered in the setup method

My Consumer is setup like

class Consumer() {

@KafkaListener(topics="X",
groupId ="...",
containerFactory="my-container-factory"
)
public void consume(ConsumerRecord<String,byte[] rec) {
 //logic to handle
   logger.info("Print rec : "+rec)
}

}

else where I've set up my ListenerContainerFactory with a name like

@Bean(name="my-container-factory")
public KafkaContainerListenerFactory<String,byte[]> factory() {
}

What could be wrong with this?My assertions in the test case fail and additionally I don't see my log statements that should be printed if my consume method were ever called.

I've a feeling,that auto configuration due to @SpringBootTest and @EmbeddedKafka is setting up some other listener container factory and so maybe my @KafkaListener annotation is wrong. I know,its a bit vague but could you please tell me what/where to look at?If I run as a @SpringBootApplication my Consumer is pulling in messages from the actual queue.So no problems with my actual app.Its the test that's not executing as per expectation.

Please help.

Edit 1: I have spring.kafka.consumer.auto-offset-reset=earliest set in my yml file.

Chetya
  • 1,267
  • 1
  • 17
  • 31
  • Show your consumer configuration. Have you set `auto-offset-reset` to `earliest` ? A common error in test cases is that messages are sent before the container starts and the default (`latest`) means the consumer won't see messages that are already in the topic. – Gary Russell Jan 31 '20 at 16:55
  • I have the setting you mention set to earliest.I have modified the original question now to reflect the same. – Chetya Jan 31 '20 at 17:01
  • It's hard to say without seeing the test case, configuration etc. DEBUG logging should help you figure out what's wrong. – Gary Russell Jan 31 '20 at 17:08
  • Thanks for the help.As suggested by you I carefully looked at the logs but still couldn't spot anything amiss.Then I just ended up adding a sleep for about 40 secs in my setup method.This was sufficient time for my consumer to kick in find the messages,consume and then once done the teardown method kicked in which was doing embeddedKafka.destroy().I guess,earlier there was some lag in the messages actually showing up by then the test method had run (without any messages seen) and the teardown was destroying the broker.Not sure,if there is a more elegant solution but this works – Chetya Feb 03 '20 at 15:39
  • I have seen such delays when there are multiple tests using the same broker with the same `group.id`. I always use a unique `group.id` in each test consumer to avoid unnecessary rebalancing. – Gary Russell Feb 03 '20 at 15:53

0 Answers0