6

I have started writing test cases to my Mule project.

I have written the functional test case for my Main Flows as follows.

public void testMainFlow_1() throws Exception{
     MuleClient client = muleContext.getClient();
            MuleMessage result = client.send(helloServiceAddress, fileAsString("SamplePayloads/input_Request.xml"), properties);
    assertNotNull("Null Result", result);           
    assertEquals(result.getPayloadAsString(), fileAsString("SampleResponses/sampleResponse.xml"));   

}

But how can I test my sub-flows. They don't have any end-points. So how can I pass payload to them and test it.

Given below is my flow config.

<flow name="main_flow" >
    ....
    ....
    <flow-ref  name="subflow_1" />
    ....
    ....
    <flow-ref  name="subflow_2" />
    ....
    ....
</flow>

<sub-flow name="subflow_1">
    ....
    <some-transformer ... />
    <out-bound call to web-service />
    <some-transformer ... />
    ....
</sub-flow>

<sub-flow name="subflow_2">
    ....
    <some-transformer ... />
    <out-bound call to web-service />
    <some-transformer ... />
    ....
</sub-flow>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
user1760178
  • 6,277
  • 5
  • 27
  • 57

3 Answers3

5

Using the FunctionalTestCase it should be as simple as:

MessageProcessor subFlow = muleContext.getRegistry().lookupObject("subflow_1");
MuleEvent result = subFlow.process(getTestEvent("test_data"));

but it doesn't work.

For now, the best approach IMO consists in having a test config that contains flow wrappers for the sub-flows you want to test and load this test config alongside your main config in the FunctionalTestCase.

@genjosanzo's approach works too but it is based on associating the sub-flow with a pre-existing main-flow from test code itself. I personally think it would be stricter to create test flows instead.

David Dossot
  • 33,403
  • 4
  • 38
  • 72
  • I have tried creating wrapper flows for the sub-flows. That is a work around. But want to try if there is something in Mule that supports testing sub-flows directly. – user1760178 Feb 06 '13 at 14:11
  • Then please upvote/follow the JIRA ticket I created and linked in my answer. – David Dossot Feb 06 '13 at 17:59
2

By using the latest Mule version we can test sub-flow with the following script:

SubflowInterceptingChainLifecycleWrapper subFlow = getSubFlow("subflowName");
subFlow.initialise();

MuleEvent event = subFlow.process(getTestEvent(""));
MuleMessage message = event.getMessage();

assertEquals(expect, message.getPayload()); 
sulthony h
  • 1,279
  • 1
  • 8
  • 9
1

Invoking a subflow from a test case is fairly simple, this is an example:

    @Test
    public void invokeSubFlow() throws Exception {
        MessageProcessor mp = (MessageProcessor) muleContext.getRegistry()
                .lookupObject("subflow_2");
        FlowConstruct parentFlow = muleContext.getRegistry().lookupFlowConstruct("main_flow");
        ((FlowConstructAware) mp).setFlowConstruct(muleContext.getRegistry()
                .lookupFlowConstruct("subflow_2"));
        Lifecycle lc = (Lifecycle) mp;
        lc.initialise();
        lc.start();
        MuleMessage muleMessage = new DefaultMuleMessage("test", muleContext);
        MuleEvent event = new DefaultMuleEvent(muleMessage,
                MessageExchangePattern.REQUEST_RESPONSE,
                new DefaultMuleSession(parentFlow,muleContext));

        mp.process(event);
    }
genjosanzo
  • 3,264
  • 2
  • 16
  • 21
  • 1
    I had to chuckle a bit at your assertion that it's "fairly simple" given the code that follows, but thanks for posting this. – Jason Wheeler Dec 22 '14 at 20:56