対象変数にデータを保存するプロセッサのテスト

結果を対象変数に保存するプロセッサをテストする場合、保存された変数の内容をアサーションで確認するようにします。プロセッサをモックする場合は、対象変数の値を定義します。

HTTP Request 操作からの対象変数のテストの例

次の XML 設定を使用する Mule アプリケーションについて考えてみます。

<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
  <http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" />

<flow name="targetVariableTestFlow" >
  <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/test"/>
  <http:request
    method="GET"
    doc:name="Request"
    config-ref="HTTP_Request_configuration"
    target="httpResponse"
    targetValue="#[message]"
    url="http://www.mulesoft.com"/>
  <logger level="INFO" doc:name="Logger" message="#[vars.httpResponse]"/>
</flow>

HTTP リスナが要求を受信すると、アプリケーションは HTTP 要求を実行し、プロセッサは受信した HTTP 応答を ​httpResponse​ という対象変数に保存します。

MUnit テストを作成し、保存された変数にたとえば ​200​ HTTP 状況コードが含まれていることをアサートできます。

<munit:test name="test-target-variable-targetVariableTestFlowTest" description="Test">
  <munit:execution >
    <flow-ref doc:name="Flow-ref to targetVariableTestFlow" name="targetVariableTestFlow"/>
  </munit:execution>
  <munit:validation >
    <munit-tools:assert-that doc:name="Assert that" expression="#[vars.httpResponse.attributes.statusCode]" is="#[MunitTools::equalTo(200)]"/>
  </munit:validation>
</munit:test>

HTTP 要求を実行せずにこのアプリケーションの動作をテストする場合は、プロセッサと保存された対象変数をモックできます。

対象変数にデータを保存するプロセッサのモック

次のテストでは、前述の Mule アプリケーション例の HTTP 要求と結果の対象変数をモックして、保存された HTTP 状況コードが ​200​ であることをアサートします。

<munit:test name="test-target-variable-targetVariableTestFlowTest" description="Test">
  <munit:behavior >
    <munit-tools:mock-when doc:name="Mock when HTTP response" processor="http:request">
      <munit-tools:with-attributes >
        <munit-tools:with-attribute attributeName='#["doc:name"]' whereValue='#["Request"]' />
      </munit-tools:with-attributes>
      <munit-tools:then-return >
        <munit-tools:variables >
          <munit-tools:variable key="#['httpResponse']" value='#[{"attributes":{"statusCode":200}}]' />
        </munit-tools:variables>
      </munit-tools:then-return>
    </munit-tools:mock-when>
  </munit:behavior>
  <munit:execution >
    <flow-ref doc:name="Flow-ref to targetVariableTestFlow" name="targetVariableTestFlow"/>
  </munit:execution>
  <munit:validation >
    <munit-tools:assert-that doc:name="Assert that" expression="#[vars.httpResponse.attributes.statusCode]" is="#[MunitTools::equalTo(200)]"/>
  </munit:validation>
</munit:test>