Contact Us 1-800-596-4880

Testing a Processor That Stores Data in a Target Variable

When you test a processor that stores its result in a target variable, ensure that the assertions check the content of the stored variable. If you mock the processor, define a value for the target variable.

Example Test of a Target Variable from an HTTP Request Operation

Consider a Mule application with the following XML configuration:

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

When the HTTP listener receives a request, the app makes an HTTP request and the processor stores the received HTTP response in a target variable called httpResponse.

You can create an MUnit test to assert that the stored variable contains, for example, a 200 HTTP status code:

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

If you want to test the behavior of this app without making the HTTP request, you can mock the processor and the stored target variable.

Mock a Processor that Stores Data in a Target Variable

The following test mocks the HTTP request and the resulting target variable from the previous Mule app example, and then asserts that the stored HTTP status code is 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>