簡體   English   中英

如何在單元測試中使用spring-cloud-stream的output綁定?

[英]How to use the output binding of spring-cloud-stream in unit tests?

我正在使用 spring cloud stream 修改 Kafka 主題並將結果數據與“.toTable()”寫入表。 在 application.yaml 中,我將設置輸入和 output 綁定。 這在 Kafka 集群上運行良好,但不適用於我當前的測試設置。

@Configuration
public class ObjectTopology {

    @Bean
    public static Serde<Object> objSerde() {
        return new ProtobufSerde<>(Object.parser());
    }

    @Bean
    public Function<KStream<String, Object>, KTable<String, Object>> obj() {

        return objKStream -> objKStream
                .transform((TransformerSupplier<String, Object, KeyValue<String, Object>>) SomeTransformer::new)
                .toTable();
    }
}

申請.yaml:

spring.cloud.stream.bindings:
    obj-in-0:
        destination: input-name
    obj-out-0:
        destination: output-name

我將如何訪問下面代碼中“toTable”生成的 KTable? 有沒有辦法在我的單元測試中使用 spring-cloud-stream 綁定?

ObjectTopology objectTopology = new ObjectTopology();
StreamsBuilder streamsBuilder = new StreamsBuilder();

Serde<String> keySerde = Serdes.String();
Serde<Object> valueSerde = objSerde();

KStream<String, Object> objKStream = streamsBuilder.stream("input-topic-name", Consumed.with(keySerde, valueSerde));

objectTopology.obj().apply(objKStream);

TopologyTestDriver topologyTestDriver = new TopologyTestDriver(streamsBuilder.build());

TestInputTopic<String, Object> objTestInputTopic = topologyTestDriver.createInputTopic("input-topic-name", keySerde.serializer(), valueSerde.serializer());
KeyValueStore<String, Object> objStore = topologyTestDriver.getKeyValueStore("???"); // I would like to use the name defined by the output binding in application.yaml "output-name"

Object object = createObject();

objTestInputTopic.pipeInput("elem_0", object);

Object result = objStore.get("elem_0");
assertThat(result).isEqualTo(object);

要在單元測試中訪問由KTable方法生成的toTable ,您可以使用TopologyTestDrivergetKeyValueStore方法來檢索與 output 綁定關聯的存儲。

在您的代碼中,您可以像這樣使用getKeyValueStore方法:

TopologyTestDriver topologyTestDriver = new TopologyTestDriver(streamsBuilder.build());
KeyValueStore<String, Object> objStore = topologyTestDriver.getKeyValueStore("output-name");

這將檢索與您的application.yaml文件中定義的 output 綁定關聯的存儲,然后您可以使用它來訪問KTable中的值。

或者,您可以使用@Output注釋將 output 綁定的MessageChannel注入到您的測試中,然后使用send方法將消息發送到 output 綁定。 然后,您可以使用TopologyTestDrivercreateInputTopic方法創建表示 output 綁定的測試輸入主題,並使用此輸入主題驗證消息是否按預期發送到 output 綁定。

以下是您如何在測試中使用@Output注釋的示例:

@Autowired
@Output("obj-out-0")
private MessageChannel outputChannel;

@Test
public void testOutputBinding() {
    // create a message to send to the output binding
    Message<Object> message = MessageBuilder.withPayload(createObject()).build();

    // send the message to the output binding
    outputChannel.send(message);

    // create a test input topic representing the output binding
    TestInputTopic<String, Object> objTestInputTopic = topologyTestDriver.createInputTopic("obj-out-0", keySerde.serializer(), valueSerde.serializer());

    // verify that the message was sent to the output binding
    assertThat(objTestInputTopic.readKeyValue()).isEqualTo(new KeyValue<>("elem_0", createObject()));
}

toTable創建一個內部主題。 您可能應該使用 topologyTestDriver.createOutputTopic 來閱讀它。

對於命名,有https://kafka.apache.org/26/javadoc/org/apache/kafka/streams/kstream/KStream.html#toTable--

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM