我正在尝试为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)
}
}
字符串
我不太清楚这个错误的原因是什么。
1条答案
按热度按时间nkoocmlb1#
这个错误是因为你正在使用
@SpringBootTest
annotation来为spring Boot 提供冲突,删除这个,你的问题就解决了,然后从jUnit
使用@RunWith
。欲了解更多信息,您可以查看此链接https://docs.spring.io/spring-kafka/reference/html/#kafka-testing-embeddedkafka-annotation