Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing sub-flows in Mule

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>
like image 827
user1760178 Avatar asked Feb 05 '13 21:02

user1760178


People also ask

What is sub flow in MuleSoft?

There are three different types of flows in Mule: Subflows – a synchronous flow inheriting the processing and exception handling strategy of the parent flow. Synchronous Flows – a synchronous flow with its processing and exception handling strategy.

How one can handle errors in a Mule sub flow?

Error Handling: Every Mule flow has it's own Error Handling section where as sub-flow doesn't have. If any error occurred in the sub-flow, it will propagate the error to the parent flow by default. The error will be handled according the implementation in the parent flow Error handling section.

What is the difference between sub flow and sync flow?

While a subflow is running, processing on the triggering flow pauses, then resumes only after the subflow completes its processing and hands the message back to the triggering flow. A synchronous flow, like a subflow, processes messages synchronously (relative to the flow that triggered its execution).


3 Answers

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.

like image 61
David Dossot Avatar answered Sep 27 '22 23:09

David Dossot


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()); 
like image 25
sulthony h Avatar answered Sep 27 '22 23:09

sulthony h


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);
    }
like image 20
genjosanzo Avatar answered Sep 27 '22 23:09

genjosanzo