我正在使用KafkaTestUtils
来获取消费者记录以及其他方便的实用程序进行验证,但是我看到当我打电话给KafkaTestUtils.getSingleRecord(..,...)
时似乎从中获取了所有记录。该主题是从其他测试方法(例如:verifyEmptinessErrorMessageInTopic()
)发送的,但在assertion
方法的getSingleRecord
失败,因为计数不是1
。在我的实际业务逻辑侦听器中,这是一个手动确认,我执行acknowledgment.acknowledge()
来提交偏移量,但是在测试代码中,它似乎从该主题而不是最后一个获取所有记录。我还尝试了consumer.commitsync()
,该操作通常执行偏移提交,但效果也不佳。
我在这里是否缺少测试实用程序中的任何配置?感谢您的输入。
private static KafkaTemplate<String, Object> kafkaTemplate;
private static Consumer<String, Object> consumerCardEventTopic;
private static Consumer<String, Object> consumerCardEventErrorTopic;
@BeforeClass
public static void setup() throws Exception {
// Producer Setup
Map<String, Object> producerConfig = KafkaTestUtility.producerProps(kafkaEmbedded);
ProducerFactory<String, Object> pf = new DefaultKafkaProducerFactory<>(producerConfig);
kafkaTemplate = new KafkaTemplate<>(pf);
// Consumer for cardEventTopic setup
Map<String, Object> consumerConfig =
KafkaTestUtils.consumerProps("cardEventGroup", "true", kafkaEmbedded);
consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
ConsumerFactory<String, Object> cf = new DefaultKafkaConsumerFactory<>(consumerConfig);
consumerCardEventTopic = cf.createConsumer();
kafkaEmbedded.consumeFromAnEmbeddedTopic(consumerCardEventTopic, cardEventTopic);
// Consumer for cardEventErrorTopic setup
Map<String, Object> consumerConfigError =
KafkaTestUtils.consumerProps("cardEventErrorGroup", "true", kafkaEmbedded);
consumerConfigError.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerConfigError.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
ConsumerFactory<String, Object> cf1 = new DefaultKafkaConsumerFactory<>(consumerConfigError);
consumerCardEventErrorTopic = cf1.createConsumer();
kafkaEmbedded.consumeFromAnEmbeddedTopic(consumerCardEventErrorTopic, cardEventErrorTopic);
}
@Test
public void verifyProcessedSuccessfully() {
kafkaTemplate.send(cardEventTopic, accountValid());
ConsumerRecord<String, Object> received =
KafkaTestUtils.getSingleRecord(consumerCardEventTopic,cardEventTopic);
assertThat(received).isNotNull();
assertThat(received.value()).isInstanceOf(String.class);
}
@Test
public void verifyEmptinessErrorMessageInTopic() {
kafkaTemplate.send(cardEventTopic, accountInValid());
ConsumerRecord<String, Object> received =
KafkaTestUtils.getSingleRecord(consumerCardEventErrorTopic,cardEventErrorTopic);
assertThat(received).isNotNull();
consumerCardEventTopic.commitSync();
}
@Test
public void testMethod3(){
}
@Test
public void testMethod4(){
}