简体   繁体   中英

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

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 {

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM