---
schema_version: "1"
id: f411ef20eed3aedaa2bea42c2812e51a1f592fae4030a727f3c5ab27490cfc25
title: Test MUnit Processors That Use Target Variables
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/latest/test-processor-that-uses-target-variable.md
canonical_url: https://docs.mulesoft.com/munit/latest/test-processor-that-uses-target-variable
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 28e19fde319f977d3999d4cdf5e0c182f15a79965f0c6f900de4b8075110e4e3
description: MuleSoft Documentation Site
release: "3.7"
latest: true
---
---
product: MUnit
version: 3.7
is-latest-version: true
---

# Test MUnit Processors That Use Target Variables

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

![logo acb active](/mulesoft/current/en-us/_/img/icons/anypoint-code-builder.svg "Anypoint Code Builder")Anypoint Code
Builder

![logo studio active](/mulesoft/current/en-us/_/img/icons/studio.svg "Anypoint Studio")Anypoint Studio

When testing a processor that stores its result in a target variable in MUnit, assert the content of
the stored variable rather than the payload. If you mock the processor, define the target variable
value in the mock configuration.

## Example Test of a Target Variable from an HTTP Request Operation

Consider a Mule application with the following XML configuration:

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

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

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

## See Also

- [Mock When Event Processor](https://docs.mulesoft.com/munit/latest/mock-event-processor)

- [MUnit Matchers](https://docs.mulesoft.com/munit/latest/munit-matchers)
