I have a spring boot test to check if a kafka consumer listens for a message in specific topic. The kafka listener is triggered when using @SpringBootTest
. But I just don't want to load all the classes and I only supplied the listener class like this @SpringBootTest(classes={KafkaConsumerTest.class})
.
When only loading the consumer class, the listener has stopped to trigger. Is there something I am missing?
Here is the KafkaTestConsumer class
@Service
public class KafkaTestConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaTestConsumer.class);
private CountDownLatch latch = new CountDownLatch(1);
private String payload;
@KafkaListener(topics = {"topic"})
public void receive(ConsumerRecord<?, ?> consumerRecord) {
payload = consumerRecord.toString();
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
public void resetLatch() {
latch = new CountDownLatch(1);
}
public String getPayload() {
return payload;
}
}
CodePudding user response:
It would be great to see what is your KafkaConsumerTest
, but perhaps you just override the whole auto-configuration with a plain @Configuration
.
See more in docs: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing.spring-boot-applications.detecting-configuration
If you want to customize the primary configuration, you can use a nested
@TestConfiguration
class. Unlike a nested@Configuration
class, which would be used instead of your application’s primary configuration, a nested@TestConfiguration
class is used in addition to your application’s primary configuration.