如何在弹簧集成中进行单元测试。

时间:2015-04-21 01:33:56

标签: unit-testing spring-integration

我是Spring集成的新手。我需要为整合图编写单元测试。该图表以gateway-> splitter-> enricher-> aggregator-> Transformer开头。因此,如果我想单独为单独的单独测试编写单元测试,我该如何进行。

我提到了this文章,但所有文章都只有一个组件。但是在这种情况下如何做到如上所述。?

1 个答案:

答案 0 :(得分:3)

目前尚不清楚为什么您引用的答案引用的测试样本对您没有帮助。流程中的内容并不重要;基本思路是将消息发送到流的开头并检查流结束时的结果,可能是用队列通道替换最终通道,您可以从测试用例中进行轮询。

你可以stop()最终的消费者,所以他没有抓住结果。

编辑:(以回应以下评论)。

您可以抢占组件的输出频道......

...

<int:channel id="toHE"/>

<int:header-enricher id="he" input-channel="toHE" output-channel="fromHE">
    <int:header name="foo" value="bar"/>
</int:header-enricher>

<int:channel id="fromHE"/>

...

然后......

@Autowired
private MessageChannel toHE;

@Autowired
@Qualifier("he.handler")
private MessageTransformingHandler headerEnricher;

@Test
@DirtiesContext
public void testEnricher() {
    PollableChannel outputChannel = new QueueChannel();
    headerEnricher.setOutputChannel(outputChannel);
    toHE.send(MessageBuilder.withPayload("baz").build());
    Message<?> out = outputChannel.receive(10000);
    assertNotNull(out);
    assertEquals("bar", out.getHeaders().get("foo"));
}