'Unit testing errors with @EmbeddedKafka and spring boot
I have IdentManagerApp with regular SpringBoot 2.6.8 and Kafka producers and consumers. Additionally I have custom Kafka configurations and failure scenarios handling with Retry/DLT topics. The respective factories creation need beans like :
ConcurrentKafkaListenerContainerFactory,
ConcurrentKafkaListenerContainerFactoryConfigurer configurer,
ConsumerFactory<Object, Object> kafkaConsumerFactoryOriginalToRetry,
KafkaTemplate<String, String> kafkaTemplate,
KafkaListenerEndpointRegistry registry,
etc
While all of that works smooth while running application, it doesnt go well while running Junits.
I keep getting errors like -
'org.springframework.boot.autoconfigure.kafka.ConcurrentKafkaListenerContainerFactoryConfigurer' that could not be found.
OR
Field registry in <package> required a bean of type 'org.springframework.kafka.config.KafkaListenerEndpointRegistry' that could not be found.
I fix one and other one pops up.
Is EmbeddedKafka compatible with exhaustive unit testing for springboot + Kafka ?
My simple test config looks like -
Main class :
@DirtiesContext
@EmbeddedKafka(partitions = 1, brokerProperties = { "listeners=PLAINTEXT://localhost:8888", "port=8888" })
@EnableKafkaCustomization
@SpringBootTest(classes = IdentManagerApp.class)
class OrchestratorAppTests {
@Test
void contextLoads() {
}
}
Ultra simple test class, doesnt even have Kafka related operations yet -
@ExtendWith(MockitoExtension.class)
@Import({
KafkaConfiguration.class
})
@ContextConfiguration
@WebMvcTest(controllers = IncomingRequestController.class)
public class IncomingRequestControllerTest { . . . . . }
Pom has below with Springboot's 2.6.8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
