我正在为Kafka编写集成测试。为此,我正在使用Spring的EmbeddedKafka
。这是我的课:
@Component
@EmbeddedKafka(
partitions = 1,
topics = {"Topic},
brokerProperties = {"listeners=PLAINTEXT://localhost:9091", "port=9091"})
public class KafkaConsumerTestUtils {
@Autowired private EmbeddedKafkaBroker embeddedKafka;
....
}
我正在用
@SpringBootTest
注释的类中自动装配此utils类。这是我得到的错误:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.kafka.test.EmbeddedKafkaBroker' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
... 66 more
奇怪的是,当我在任何测试类中自动连接
EmbeddedKafkaBroker
时,都会创建它并且测试运行正常。有人可以帮我弄这个吗?
最佳答案
不能完全确定为什么,但是您似乎没有按照预期的方式使用它。
@EmbeddedKafka是可以在运行基于Spring Kafka的测试的测试类上指定的注释。
文档状态:
The typical usage of this annotation is like:
@RunWith(SpringRunner.class)
@EmbeddedKafka
public class MyKafkaTests {}