---
schema_version: "1"
id: 0892e89e6df7e9e52e824892a7feb7027315e31166a9e9934c159896d49ca957
title: Mocking a Message Before and Inside a Foreach Processor
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/2.3/mock-before-after-foreach-cookbook.md
canonical_url: https://docs.mulesoft.com/munit/2.3/mock-before-after-foreach-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b1fe6139ea16db26f948f3c8d9891252662a6472bb1a902a8fae9ef929683547
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
---
product: MUnit
version: 2.3
---

# Mocking a Message Before and Inside a Foreach Processor

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Suppose you have a processor inside a `foreach` scope and you want to provide different values on
each iteration:

```xml
<flow name="foreachFlow">
	<foreach doc:name="For Each">
		<flow-ref doc:name="Flow Reference" name="FlowToMock"/>
	</foreach>
</flow>
```

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:

```xml
<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:

```xml
<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>
```

## Mock 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:

```none
fun createMessage(payload: Any, attributes: Any = null)

fun createMessage(payload: Any, mimeType: String|Null , attributes: Any, attributeMimeType: String|Null)
```

```xml
<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( &quot;ITEM-1&quot;,  &quot;text/plain&quot;, { property : 'ATTRIBUTE-1'}, null) , MunitTools::createMessage( &quot;ITEM-2&quot;,  &quot;text/plain&quot;, { property : 'ATTRIBUTE-2'}, null)]]" />
   </munit-tools:then-return>
</munit-tools:mock-when>
```

## See Also

- [Mock When Event Processor](https://docs.mulesoft.com/munit/2.3/mock-event-processor)
