Spring Boot 和 Kafka 升级破坏了我的测试

标签 spring spring-boot kotlin spring-kafka spring-kafka-test

我更新了一个项目的库,特别是我更新了 Spring Boot,从版本 2.2.6 到 2.3.2。

the migration documentation 中所述,这也意味着我的 Kafka 依赖项发生了变化,升级到 Spring Kafka 和 Kafka 2.5

但是,由于此更改,我的一些测试“随机”失败,例如,当我执行所有测试时,我在其中一些测试中遇到了这种失败(但并非总是在相同的测试中):

java.lang.IllegalStateException: More than one record for topic found

来自这一行:

KafkaTestUtils.getSingleRecord(consumer, topicConfiguration.getTopic(event))

文档中没有提及发生这种情况的原因,也没有其他日志错误消息阐明发生这种情况的原因。由于我没有更改我的应用程序的逻辑,我是否在迁移过程中遗漏了什么?

如果我尝试这样的事情:

KafkaTestUtils.getRecords(consumer)
    .records(topicConfiguration.getTopic(event)).map { it }[0]

为了只获得第一个,之前有效的测试将因索引而失败并出现 IndexOutOfBoundsException

最佳答案

根据test documentation在 Spring Kafka 库中,从 2.5 版开始,他们改变了配置消费者的方式。这是确切的引述:

Starting with version 2.5, the consumerProps method sets the ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to earliest. This is because, in most cases, you want the consumer to consume any messages sent in a test case. The ConsumerConfig default is latest which means that messages already sent by a test, before the consumer starts, will not receive those records. To revert to the previous behavior, set the property to latest after calling the method.

这很可能是描述的测试错误的原因。

关于Spring Boot 和 Kafka 升级破坏了我的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63409601/

相关文章:

kotlin - Groovy 的 findIndexValues 是否有 Kotlin 等价物?

android - 将项目添加到 LiveData 列表时通知观察者

spring - 如何使用 spring 生成没有密码的 oauth token

java - Spring数据ldap配置

spring-boot - Spring Cloud Contract - 查询参数契约(Contract)始终与同一契约(Contract)匹配

java - java(spring boot)中的aws lambda函数无法创建文件?

Spring Webflux带注释的rest Controller 不支持ServerHttpRequest作为方法参数: java. lang.NoSuchMethodException

spring - 使用 @Transactional 时此行上有多个标记

java - 设置蒲公英数据表中的可见行数

android-studio - 您可以在Kotlin中使用自定义getter,默认setter和没有初始化程序声明属性吗?