Testing sub-flows in Mule

前提是你 提交于 2019-12-03 14:28:34

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.

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()); 

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!