2017-10-17 194 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,你可以根據自己的需要進行調整。