6

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 {
pixel
  • 24,905
  • 36
  • 149
  • 251
  • You can use @Embeddedkafka annotation or EmbeddedKafkaRule? – Hatice Oct 18 '19 at 10:20
  • @Hatice I want to do the opposite - in my test I don't need kafka thus - I want to have a way to easily disable it. – pixel Oct 18 '19 at 11:06
  • 1
    You don't need `@EnableKafka` there - Boot automatically configures it for you if spring-kafka is on the classpath; so it's redundant there. To disable it for a test you need to disable auto configuration for kafka. Refer to the [Boot documentation](https://docs.spring.io/spring-boot/docs/2.2.0.RELEASE/reference/html/). – Gary Russell Oct 18 '19 at 12:02

3 Answers3

18

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")
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
6

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

Hatice
  • 874
  • 1
  • 8
  • 17
4

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

@EnableAutoConfiguration(exclude = {KafkaAutoConfiguration.class})

masterxilo
  • 2,503
  • 1
  • 30
  • 35
slorinc
  • 2,264
  • 1
  • 11
  • 8