Mock When イベントプロセッサ

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

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

<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​ を作成します。

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

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

<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​ を使用して返された値を経時的に変更する場合にフローを呼び出したり、特定の入力に応じて異なる応答を使用したりできます。

Then-Return を使用したモック

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

<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​ プロセッサを設定します。

<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​ 操作へのコールをモックします。

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

<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 リクエスタがフロー内にあるとします。

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

<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>
1 フロー内のリクエスタの設定で HTTP 要求をモックします。
2 HTTP:CONNECTIVITY​ エラーをスローするように then-return 要素を設定します。
これにより、アプリケーション内の On-error スコープがトリガされます。
3 リクエスタを含むフローを実行します。
4 返されたペイロードが On-error スコープ内で設定したものであるとアサートします。

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

パラメータ

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

  • TypeId​: Mule エラー種別の定義に従います。たとえば、上記で説明した HTTP:CONNECTIVITY エラーです。

  • Cause​: エラーのメッセージを含む例外のインスタンスを期待します。たとえば、Java を使用して、次の例外を記述します。

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

Then-Call を使用したモック

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

<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>
1 これがモックするプロセッサです。
2 このフローは、受信イベントに応じて変化する変数を設定するモックによってコールされます。
3 目的のフローをコールするようにモックを設定します。