0

I create a new spring boot project with a simple test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleApplicationTests {

    @Test
    public void contextLoads() {
    }

}

When I run this test it succeeds. But If I add any method annotated @KafkaListener annotation to any service:

@KafkaListener(topics = "test", groupId = "v-group")
public void test(){
  log.info("test");
}

And run test, It works sometimes and throws an exception:

Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata
ip696
  • 6,574
  • 12
  • 65
  • 128
  • Looks like a problem with your kafka configuration, perhaps mis-matched SSL properties; you need to show the application.yml (or .properties). – Gary Russell May 14 '19 at 13:43
  • @ Gary Russell I have empty `application.yml `. Need I have a correct config to Kafka if I run a unit test and I not test Kafka? – ip696 May 14 '19 at 14:30
  • By default, we try to connect to kafka on `localhost:9092`. You either need a real broker or use the embedded broker provided by the framework, – Gary Russell May 14 '19 at 15:00
  • Do I understand correctly - if I add `@KafkaListener ` to service and try run `UnitTest`(Or integration test in this case: @SpringBootTest) - SpringBootTest load context and can not connect to Kafka and fail? – ip696 May 14 '19 at 15:06

1 Answers1

1

By default, when the application context loads, the framework will start() the listener container for the listener.

You can set the autoStartup property to false to prevent the container from starting.

@KafkaListener(topics = "test", groupId = "v-group", autoStartup = "false")
public void test(){
  log.info("test");
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179