ip696
ip696

Reputation: 7084

SpringBootTest fail if application contains @KafkaListener annotation

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

Upvotes: 0

Views: 703

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

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");
}

Upvotes: 1

Related Questions