I am using spring kafka to consume message from kafka topic, so I have a kafka consumer configuration class:
@Configuration
class KafkaConfiguration {
// kafka consumer configurations
}
I have some JUnit tests which will load spring context with mockMvc to test my API, I don't want to test features related to kafka messaging, how can I stop kafka from consuming message only for JUnit tests? It keep failing because I don't have a kafka server at my local and CI environment.
Spring profile is not a very good option, because I will have to write code like:
@Configuration
@Profile("!unit-test")
class KafkaConfiguration {
//kafka configuration
}
which I will end up with production code written only for testing purpose, not very clean, is there other way I can do to disable kafka for tests?
Add @ConditionalOnProperty annotation on configuration class
Example:
@ConditionalOnProperty(value = "kafka.enable", havingValue = "true", matchIfMissing = true)
, and add application.properties file property kafka.enable=false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With