Search code examples
javaunit-testingmulefunctional-testing

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>

Solution

  • 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.