I am facing a very annoying problem, during test process in my Spring Boot application. I have an application that uses Kafka Streams and declares them in a dedicated configuration file.
@EnableKafka
@EnableKafkaStreams
@Configuration
public class KafkaStreamConfiguration {
@Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
public StreamsConfig kStreamsConfigs() {
// Omissis
}
@Bean
public KStream<String, String> kStream() {
// Omissis
}
}
My application also exposes a dedicated REST API using a Spring @RestController
. I want to test this rest controller in isolation, with something like the following.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class RestControllerTest {
@Test
public void someFancyTest() {
// Omissis
}
}
The problem is that I cannot avoid Spring context to start the streams defined in KafkaStreamConfiguration
class. I did not find any way to exclude this class from Spring context during the execution of RestControllerTest
.
I do not want to declare a KafkaEmbedded
instance in RestControllerTest
class. It seems to me a nonsense.
Is it possible? How can I slice tests to maintain some order of independence?
The application class is as simple as possible.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I am using Spring Boot version 1.5.8 and Kafka version 0.10.2.1.