---
schema_version: "1"
id: eb39c8081bd91eb89c975bd06be28b59b15403d43f71b4d4a19e6955c3d1f39d
title: Mock When イベントプロセッサー
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/munit/2.3/mock-event-processor.md
canonical_url: https://docs.mulesoft.com/jp/munit/2.3/mock-event-processor
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1e869ff06dffbe3816c7c2f7e0c46801060fb9f69ba9bf061664c8df2729d088
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
# Mock When イベントプロセッサー

**Mock When**​ プロセッサーでは、定義された名前と属性に一致したときにイベントプロセッサーをモックできます。

たとえば、​**Mock Event**​ プロセッサーを使用して、モックペイロードで POST 要求をモックできます。

```xml
<munit-tools:mock-when processor="http:request">
   <munit-tools:with-attributes>
       <munit-tools:with-attribute attributeName="method" whereValue="#['POST']"/>
   </munit-tools:with-attributes>
   <munit-tools:then-return>
       <munit-tools:payload value="#['mockPayload']"/>
   </munit-tools:then-return>
</munit-tools:mock-when>
```

モックするプロセッサーを定義するように ​**processor**​ 属性を設定し、属性の名前と値を定義するように ​**with-attribute**​ 要素を設定できます。上記の例では、POST メソッドを定義します。

DataWeave の機能とマッピングを使用して、​**then-return**​ 要素の ​`value`​ 属性を設定することもできます。たとえば、​**src/test/resources/sample\_data**​ ファイル内に ​**mockPost.dwl**​ を作成します。

```none
%dw 2.0
output application/json
---
{
	"foo" : "var"
}
```

次の DataWeave ファイルは、POST 要求で送信するペイロードを作成します。その後、​**readUrl**​ DataWeave 機能を使用してマッピングファイルを読み込むことができます。

```xml
<munit-tools:then-return>
  <munit-tools:payload value="#[readUrl('classpath://sample_data/mockPost.dwl')]" mediaType="application/json" encoding="UTF-8" />
</munit-tools:then-return>
```

最後に、モックプロセッサーが返す応答種別を定義するように ​**then-return**​ 要素を設定できます。ペイロード、変数、属性のリスト、またはエラーにできます。

**then-return**​ を使用して静的な定数値を返すか、​**then-call**​ を使用して返される値が経時的に変化する可能性がある場合にフローを呼び出したり、特定の入力に応じて異なる応答を使用したりできます。

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">プロセッサーのモックは Mule アプリケーションの初期化後に実行され、プロセッサーがモックされる場合であっても、一部のコネクタでアプリケーションを起動するにはログイン情報または設定が必要な場合があります。</td></tr></tbody></table>

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">Logger プロセッサーや Transform Message プロセッサー (DataWeave) はモックできません。</td></tr></tbody></table>

## [](#then-return-を使用したモック)Then-Return を使用したモック

たとえば、次のような Web サービスコンシューマーをモックできます。

```xml
<wsc:config name="Web_Service_Consumer_Config">
  <wsc:connection wsdlLocation="tshirt.wsdl" service="TshirtService" port="TshirtServicePort" address="http://tshirt-service.cloudhub.io"/>
</wsc:config>

<wsc:consume config-ref="Web_Service_Consumer_Config" operation="OrderTshirt"/>
```

次の例で示しているように、​**mock-when**​ プロセッサーを設定します。

```xml
<munit-tools:mock-when processor="wsc:consume">
    <munit-tools:with-attributes>
        <munit-tools:with-attribute attributeName="operation" whereValue="#['OrderTshirt']"/>
    </munit-tools:with-attributes>
</munit-tools:mock-when>
```

この ​**mock-when**​ プロセッサーは、WSDL 定義内の ​**OrderTshirt**​ 操作へのコールをモックします。

特定の変数をモックすることもできます。

```xml
<munit-tools:mock-when processor="http:request">
	<munit-tools:with-attributes>
	    <munit-tools:with-attribute attributeName="config-ref" whereValue="#['HTTP_Request_configuration']"/>
	</munit-tools:with-attributes>
	<munit-tools:then-return>
	  <munit-tools:variables>
	  	<munit-tools:variable key="aVariable" value="#['aValue']"/>
	  </munit-tools:variables>
	</munit-tools:then-return>
</munit-tools:mock-when>
```

### [](#モックエラー)モックエラー

**Mock When**​ プロセッサーは、操作でのエラーをモックすることもできます。接続が失敗した場合に接続エラーをキャッチしてカスタムペイロードを返す、On-Error スコープを含む HTTP リクエスターがフロー内にあるとします。

```xml
<http:request-config name="HTTP_Request_Config">
    <http:request-connection host="localhost" port="8888"/>
</http:request-config>

<http:listener-config name="HTTP_Listener_Config">
    <http:listener-connection host="0.0.0.0" port="8081"/>
</http:listener-config>

<flow name="api-request">
    <http:listener config-ref="HTTP_Listener_Config" path="/"/>
    <http:request method="GET" config-ref="HTTP_Request_Config" path="/api"/>
    <error-handler>
        <on-error-continue enableNotifications="true" logException="true" type="HTTP:CONNECTIVITY">
            <set-payload value="#['Connection Error']"/>
        </on-error-continue>
    </error-handler>
</flow>
```

HTTP 要求が失敗するたびに、アプリケーションはカスタムペイロードを返すとアサートできます。

```xml
<munit:test name="HTTP-fail-test" description="Asserts Custom Payload in HTTP Connectivity errors.">
    <munit:behavior>
        <munit-tools:mock-when processor="http:request">
            <munit-tools:with-attributes>
                <munit-tools:with-attribute attributeName="config-ref"
                                            whereValue="#['HTTP_Request_Config']"/> (1)
            </munit-tools:with-attributes>

            <munit-tools:then-return>
                <munit-tools:error typeId="#['HTTP:CONNECTIVITY']"/> (2)
            </munit-tools:then-return>
        </munit-tools:mock-when>
    </munit:behavior>

    <munit:execution>
        <flow-ref name="api-request"/> (3)
    </munit:execution>

    <munit:validation>
      <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalToIgnoringCase('connection error')]"/> (4)
    </munit:validation>
</munit:test>
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td>フロー内のリクエスターの設定で HTTP 要求をモックします。</td></tr><tr><td><i class="conum" data-value="2"></i><b>2</b></td><td><strong>HTTP:CONNECTIVITY</strong>​ エラーをスローするように then-return 要素を設定します。<br>これにより、アプリケーション内の On-error スコープがトリガーされます。</td></tr><tr><td><i class="conum" data-value="3"></i><b>3</b></td><td>リクエスターを含むフローを実行します。</td></tr><tr><td><i class="conum" data-value="4"></i><b>4</b></td><td>返されたペイロードが On-error スコープで設定したものであるとアサートします。</td></tr></tbody></table>

Mule アプリケーションのすべての既知のエラー種別を返すことはできません。現在のフローで使用するモジュールで定義されている種別のエラーを返すことができます。テスト中のフローのスコープ外のエラー種別を返そうとすると、設定されたエラーではなく、​**MULE:UNKNOWN**​ エラーが返されます。

### [](#パラメーター)パラメーター

モックエラーを作成する場合は、次のパラメーターのいずれかを選択して ​**\[Then-return\]**​ タブでのエラーのトリガーを提供します。

- **TypeId**​: Mule エラー種別の定義に従います。たとえば、上記で説明した HTTP:CONNECTIVITY エラーです。
- **Cause**​: エラーのメッセージを含む例外のインスタンスを期待します。たとえば、Java を使用して、次の例外を記述します。

```java
#[java!java::lang::Exception::new("Exception Message")]
```

## [](#then-call-を使用したモック)Then-Call を使用したモック

変数を動的にモックするには、次のように ​**then-call**​ を使用します。

```xml
<flow name="flow-to-be-mocked">
    <set-variable variableName="count" value="#[0]"/>
</flow>

<flow name="flow-to-test">
    <flow-ref name="flow-to-be-mocked"/> (1)
</flow>

<flow name="count-to-3"> (2)
    <choice>
        <when expression="#[vars.count == null]">
            <set-variable variableName="count" value="#[1]" doc:name="Create counter"/>
        </when>

        <when expression="#[vars.count &lt; 3]">
            <set-variable variableName="count" value="#[vars.count + 1]"
                          doc:name="Increase count by 1, up to 3"/>
        </when>
    </choice>
</flow>

<munit:test name="variable-mock-test">
    <munit:behavior>
        <munit-tools:mock-when processor="mule:flow-ref">
            <munit-tools:with-attributes>
                <munit-tools:with-attribute attributeName="name" whereValue="#['flow-to-be-mocked']"/>
            </munit-tools:with-attributes>

            <munit-tools:then-call flow="count-to-3"/> (3)
        </munit-tools:mock-when>
    </munit:behavior>

    <munit:execution>
        <flow-ref name="flow-to-test"/> <!-- 1 -->
        <flow-ref name="flow-to-test"/> <!-- 2 -->
        <flow-ref name="flow-to-test"/> <!-- 3 -->
        <flow-ref name="flow-to-test"/> <!-- 3 -->
    </munit:execution>

    <munit:validation>
        <munit-tools:assert-that expression="#[vars.count]" is="#[MunitTools::equalTo(3)]"/>
    </munit:validation>
</munit:test>
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td>これがモックするプロセッサーです。</td></tr><tr><td><i class="conum" data-value="2"></i><b>2</b></td><td>モックによってこのフローがコールされます。モックによって、受信イベントに応じて変化する変数が設定されます。</td></tr><tr><td><i class="conum" data-value="3"></i><b>3</b></td><td>目的のフローをコールするようにモックを設定します。</td></tr></tbody></table>

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

- [Assert That イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/assertion-event-processor)
- [Fail イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/fail-event-processor)
- [Run Custom イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/run-custom-event-processor)
- [Set イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/set-event-processor)
- [Verify イベントプロセッサー](https://docs.mulesoft.com/jp/munit/2.3/verify-event-processor)