---
schema_version: "1"
id: e9102d5444796ac35a12f7a194bfc7b954b5a74abb8e0fca37df9712d0c19cc3
title: Foreach プロセッサーの前および内部のメッセージのモック
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/munit/2.3/mock-before-after-foreach-cookbook.md
canonical_url: https://docs.mulesoft.com/jp/munit/2.3/mock-before-after-foreach-cookbook
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 04806c6dcfe4234a701374a52ef1a613936f2d3a716800108f6a93159e8e3398
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
# Foreach プロセッサーの前および内部のメッセージのモック

`foreach`​ スコープ内にプロセッサーがあり、反復ごとに異なる値を提供するとします。

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

`munit-tools:mock-when`​ プロセッサーを使用してフロー内のプロセッサーをモックし、​`then-call`​ 操作を使用して ​`foreach`​ 状況に応じてペイロードを設定する新しいフローをコールします。

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

`then-call`​ によってコールされたフロー内で、​`Choice`​ プロセッサーを使用して値を評価し、モックペイロードを設定できます。この場合、​`foreach`​ カウンターが評価され、設定するペイロードが決定されます。

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

## [](#mule-メッセージのモック)Mule メッセージのモック

`file:list`​ や ​`ftp:list`​ などのプロセッサーをモックするには、Mule メッセージコレクションをペイロードとして返す必要があります。​`MunitTools`​ には、これらのペイロードをモックする機能が用意されています。

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

## [](#関連情報)関連情報

- [Mock When イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/mock-event-processor)