2

I am new to spring integration. I need to write unit test for the integeration graph. This graph starts with gateway->splitter->enricher->aggregator->Transformer. So if i want to write a unit test for enricher alone, how do i need to do it.?

I referred this article, but all of them has only one component. But how to do it in this case as above.?

Community
  • 1
  • 1
Manoj
  • 5,707
  • 19
  • 56
  • 86

1 Answers1

3

It's not clear why the testing samples referenced by your cited answer don't help you. It doesn't matter what's in the flow; the basic idea is to send a message into the start of the flow and examine the result from the end of the flow, perhaps by replacing the final channel with a queue channel, which you can poll from your test case.

You can stop() the ultimate consumer so he doesn't grab the result.

EDIT: (in response to comments below).

You can preempt the component's output channel...

...

<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"/>

...

And then...

@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"));
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • If i want to test enricher alone, i have to put message to that channel and the output automatically flows to aggregator in my example, how to re-route and examine..? – Manoj Apr 21 '15 at 15:20
  • Thank you.. So the testEnricher() will make sure the message will not go to channel fromHE and instead go to the output channel defined in testEnricher? ... Will the flow be stopped? – Manoj Apr 21 '15 at 19:40
  • Yes, the test replaces the oc with the test channel. So there's nowhere else for the reply to go. The @DirtiesContext restores everything for other tests in the test class. – Gary Russell Apr 21 '15 at 19:48