【问题标题】:How to unit test Spring Integration flow - specifically http outbound gateway如何对 Spring Integration 流程进行单元测试 - 特别是 http 出站网关
【发布时间】:2014-09-15 16:24:54
【问题描述】:

我想知道是否有人可以提供帮助 - 这已经让我发疯了好几天!

我有一个相当简单的 Spring 集成文件,我想对其进行单元测试。 SI 使用 http 出站网关,我特别想进行单元测试而不是集成测试 - 我不想提供使用 Spark 或 MockRestServiceServer 之类的模拟 http 服务器。

我的 SI 配置如下所示:

<int:channel id="modifiedAttractionChannel" datatype="u.o.n.p.i.a.s.AttractionUuid">
    <int:interceptors>
        <int:wire-tap channel="attractionModifiedChannelLogging"/>
    </int:interceptors>
</int:channel>

<int-http:outbound-gateway
        id="serviceGateway" 
        request-channel="modifiedAttractionChannel"
        url="/attractions/{uuid}" 
        http-method="GET"
        expected-response-type="u.o.n.p.i.a.v.m.Attraction"
        charset="UTF-8"
        reply-timeout="${vader.reply.timeout}"
        request-factory="clientHttpRequestFactory"
        reply-channel="vaderAttractionChannel">
    <int-http:uri-variable name="uuid" expression="headers['#{T(u.o.n.p.i.a.s.AttractionsImportInitializer).HEADER_UUID}'].value" />
</int-http:outbound-gateway>

<int:channel id="attractionChannel" datatype="u.o.n.p.i.a.v.m.Attraction">
    <int:interceptors>
        <int:wire-tap channel="vaderAttractionChannelLogging"/>
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter 
        id="attractionModifiedChannelLogging"
        expression="'attractionModifiedChannel: header=' + headers['#{T(u.o.n.p.i.a.s.AttractionsImportInitializer).HEADER_UUID}'].value + ', payload=' + payload" 
        level="INFO" />

<int:logging-channel-adapter 
        id="vaderAttractionChannelLogging" 
        expression="'attractionModifiedChannel: header=' + headers['#{T(u.o.n.p.i.a.s.AttractionsImportInitializer).HEADER_UUID}'].value + ', payload=' + payload" 
        level="INFO" />

我编写了一个单元测试,它连接了一个基本的 spring 上下文,并且能够获得 modifiedAttractionChannelsend 一个适当构建的 Message 以及 AttractionUuid 有效负载和标头值。

我的单元测试可以断言写入attractionModifiedChannelLogging 的日志消息与我预期的一样(我创建了AttractionUuidMessage,所以我知道有效负载和标头值)

我现在需要做的是断言写入vaderAttractionChannelLogging 窃听器的值。 IE。我需要用给定的标头值声明一条消息 - 没问题,我创建了标头值作为测试的一部分 - 还有有效负载。

在这种情况下,有效负载是出站网关的输出。鉴于这是一个单元测试并且我不希望对其他任何东西有任何依赖,我提供了一个模拟 ClientHttpRequestFactory,而后者又通过模拟 ClientHttpRequest 提供了一个模拟 ClientHttpResponse

这很有效,因为我可以控制出站网关否则会收到的响应正文。但是,RestTemplate 调用其HttpRequestExecutingMessageHandler 以便通过MessageConverters 将响应正文转换为对象

虽然这适用于 SI 流的执行,但这意味着回复通道 vaderAttractionChannel 上的 Attraction 实例不受测试控制;因此我无法就vaderAttractionChannelLogging窃听器上记录的内容做出任何断言。

认为解决此问题的一种方法是能够连接模拟或存根MessageConvertor,而不是返回固定Attraction 实例的标准集。但我这辈子都不知道怎么做!

注意:上面的场景是我实际尝试做的一个非常简化的版本。我并不是真的想围绕记录的值编写单元测试!我需要测试我的 SI 的整体流程,并且能够控制出站网关返回的 Attraction 的实例非常关键。

对此的任何帮助将不胜感激;
干杯

内森

【问题讨论】:

  • 不清楚您的问题是什么; “...RestTemplate 调用它的HttpRequestExecutingMessageHandler...” - 反过来。如果您真的想对流程进行单元测试,您应该提供标准转换器转换的模拟结果的正常结果。如果您真的想模拟转换,请使用message-converters 属性。
  • 谢谢加里。在这种情况下,我确实想模拟标准转换器,因为出站网关的输出对象在 SI 流的下游使用。您使用message-converters 的建议对我有用,因此感谢您的建议。如果您想用您的建议回答这个问题,我很乐意将其标记为已接受的答案。再次感谢;

标签: unit-testing mockito spring-integration


【解决方案1】:

不清楚您的问题是什么; “... RestTemplate 调用它的 HttpRequestExecutingMessageHandler ...” - 实际上是相反的。如果你真的想对流程进行单元测试,你应该提供一个标准转换器转换的模拟结果的正常结果。如果您真的也想模拟转换,请使用 message-converters 属性。

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2013-08-26
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多