junit 无法自动导丝,未找到'EmbeddedKafkaBroker'类型的Bean

k4emjkb1  于 10个月前  发布在  Kafka
关注(0)|答案(1)|浏览(96)

我正在尝试为Kafka producer编写单元测试。但是我在lateinit var embeddedKafkaBroker: EmbeddedKafkaBroker行中得到一个错误-Could not autowire. No beans of 'EmbeddedKafkaBroker' type found.
这是我的代码

@ActiveProfiles("test")
@EmbeddedKafka(
   partitions = 1,
   bootstrapServersProperty = "spring.kafka.bootstrap-servers",
   topics = arrayOf("someTopic"))
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = ["kafka.schema-registry.url=mock://mock"]
)
internal class KafkaProducerServiceTest {

   @Autowired
   lateinit var embeddedKafkaBroker: EmbeddedKafkaBroker // Error line - Could not autowire. No beans of 'EmbeddedKafkaBroker' type found.

   @Autowired
   lateinit var producer: KafkaProducerService<EventKey, EventValue>

   @Test
   fun send() {
     val mockKey = mockk<EventKey>(relaxed = true)
     val mockValue = mockk<EventValue>(relaxed = true)

     producer.send(mockKey, mockValue)
   }
}

字符串
我不太清楚这个错误的原因是什么。

nkoocmlb

nkoocmlb1#

这个错误是因为你正在使用@SpringBootTest annotation来为spring Boot 提供冲突,删除这个,你的问题就解决了,然后从jUnit使用@RunWith
欲了解更多信息,您可以查看此链接https://docs.spring.io/spring-kafka/reference/html/#kafka-testing-embeddedkafka-annotation

相关问题