2017-10-17 179 views
2

我试图通过设置Kafka服务器并使用生产者发送消息来在本地测试我的代码,但我想知道是否有一种方法可以为此编写单元测试一段代码(测试消费者收到的消息是否正确)。Akka Streams Kafka - 消费者的单元测试

val consumerSettings = ConsumerSettings(system, 
    new ByteArrayDeserializer, new StringDeserializer) 
    .withBootstrapServers("localhost:9092") 
    .withGroupId("group1") 
    .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest") 

val done = Consumer.committableSource(consumerSettings, 
    Subscriptions.topics("topic1")) 
    .map { msg => 
    msg.committableOffset.commitScaladsl() 
    } 
    .runWith(Sink.ignore) 

回答

1

你可以用下列工具测试代码:

Akka Streams Kafka项目在其自己的测试中使用了上述内容。看看它的IntegrationSpec,你可以根据自己的需要进行调整。