<flow name="foreachFlow">
<foreach doc:name="For Each">
<flow-ref doc:name="Flow Reference" name="FlowToMock"/>
</foreach>
</flow>
Mocking a Message Before and Inside a Foreach Processor
Suppose you have a processor inside a foreach
scope and you want to provide different values on each iteration:
Use an munit-tools:mock-when
processor to mock the processor in your flow and use the then-call
operation to call a new flow that set payloads depending on the foreach
status:
<munit-tools:mock-when doc:name="Mock when" processor="flow-ref">
<munit-tools:with-attributes >
<munit-tools:with-attribute attributeName="name" whereValue="FlowToMock" />
</munit-tools:with-attributes>
<munit-tools:then-call flow="MockedFlow" />
</munit-tools:mock-when>
Inside the flow that is called by the then-call
you can use a Choice
processor to evaluate a value and set a mocked payload. In this case, the foreach
counter is evaluated to decide which payload to set:
<flow name="MockedFlow" >
<choice doc:name="Choice">
<when expression="#[vars.counter == 1]">
<set-payload value='#["MOCKED1"]' doc:name="Set Payload" />
</when>
<when expression="#[vars.counter == 2]">
<set-payload value='#["MOCKED2"]' doc:name="Set Payload" />
</when>
<otherwise >
<set-payload value='#["DEFAULT"]' doc:name="Set Payload" />
</otherwise>
</choice>
</flow>
Mocking a Mule Message
To mock processors like file:list
or ftp:list
, you must return a Mule message collection as payload. MunitTools
provides a function to mock these payloads:
fun createMessage(payload: Any, attributes: Any = null)
fun createMessage(payload: Any, mimeType: String|Null , attributes: Any, attributeMimeType: String|Null)
<flow doc:name="Flow" >
<file:list doc:name="List" config-ref="File_Config" directoryPath="/tmp/"/>
</flow>
<munit-tools:mock-when processor="file:list">
<munit-tools:with-attributes>
<munit-tools:with-attribute attributeName="doc:name" whereValue="List"/>
</munit-tools:with-attributes>
<munit-tools:then-return>
<munit-tools:payload value="#[[MunitTools::createMessage( "ITEM-1", "text/plain", { property : 'ATTRIBUTE-1'}, null) , MunitTools::createMessage( "ITEM-2", "text/plain", { property : 'ATTRIBUTE-2'}, null)]]" />
</munit-tools:then-return>
</munit-tools:mock-when>