How to disable `@EnableKafka` in Spring Boot tests?

Viewed 7596

I would like to run my integration tests but I don't know how to disable @EnableKafka.

My app looks like that:

@SpringBootApplication
@EnableKafka
public class MyApplication {
3 Answers

Spring Boot come with an auto-configuration for Spring Kafka, therefore you don't need to use an explicit @EnableKafka. What you need to do in your test is just exclude KafkaAutoConfiguration:

@SpringBootTest("spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration")

You can create another config file for your Kafka configuration.

@ConditionalOnProperty("kafka.enabled")
@EnableKafka
@Configuration
public class KafkaConfiguration { }

like that and then you can disable this property in your test.properties file.

Check this solution

You can disable the autoconfiguration of Kafka with this Spring annotation:

@EnableAutoConfiguration(exclude = {KafkaAutoConfiguration.class})

Related