---
schema_version: "1"
id: d5d4a8b043095933ef7659e94f12d43e2a30d392c5655a3ea7702272b89964e1
title: Mock When Event Processor
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/3.6/mock-event-processor.md
canonical_url: https://docs.mulesoft.com/munit/3.6/mock-event-processor
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c805e1b9435406e38a4e615297a031861a5ba7f995fef70e3d73ca6fb5082182
description: MuleSoft Documentation Site
release: "3.6"
latest: false
---
---
product: MUnit
version: 3.6
keywords: munit, testing, unit testing
---

# Mock When Event Processor

> 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

The **Mock When** processor enables you to mock an event processor when it matches the defined name
and attributes.

For example, you can use the **Mock Event** processor to mock a POST request with a mocked payload:

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

You can set the **processor** attribute to define the processor to mock and set the
**with-attribute** element to define the attribute name and value. In the previous example, you
define a POST method.

You can also use DataWeave functions and mappings to set the `value` attribute in the
**then-return** element. For example, create a **mockPost.dwl** in the
**src/test/resources/sample\_data** file:

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

This DataWeave file creates the payload to send in a POST request. You can then use the **readUrl**
DataWeave function to read the mapping file:

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

Finally, you can configure a **then-return** element to define the type of response the mocked
processor returns. It can be a payload, a variable, a list of attributes, or an error.

You can use either a **then-return** to return a static, constant value, or you can use a
**then-call** to invoke a flow for cases where the returned value can change over time, or to have
different responses according to certain inputs.

> [!NOTE] The mocking of the processors is executed after the initialization of the Mule app, and
> some connectors can require credentials or configurations to start the app, even when its
> processors are mocked.

> [!NOTE] Logger processor and Transform Message processor (DataWeave) cannot be mocked.

## Mock Using Then-Return

For example, you can mock a web service consumer such as this one:

```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"/>
```

By configuring the **mock-when** processor as shown in the following example:

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

This **mock-when** processor mocks a call to the **OrderTshirt** operation in the WSDL definition.

You can also mock specific variables:

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

The **Mock When** processor also enables you to mock errors in your operations. Assume that you have
an HTTP requester in your flow with an On-Error scope that catches any connectivity errors and
returns a custom payload if the connection fails:

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

You can assert that every time the HTTP request fails, your application returns your custom payload:

```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 data-value="1"></i><b>1</b></td><td>Mock an HTTP Requester with the configuration of the requester in your flow.</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>Configure a then-return element to throw an <strong>HTTP:CONNECTIVITY</strong> error.<br>This triggers the On-error scope in your application.</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>Execute the flow containing the requester.</td></tr><tr><td><i data-value="4"></i><b>4</b></td><td>Assert that the returned payload is the one you set in the On-error scope.</td></tr></tbody></table>

You cannot return all known error types of a Mule application. You can return the type of errors
defined in the modules the current flow uses. If you try to return an error type that is out of the
scope of the flow you’re testing, you get a **MULE:UNKNOWN** error instead of the configured one.

### Parameters

If you create a mock error, choose one of the following parameters to provide the trigger for the
error in the **Then-return** tab:

- **TypeId**: Follows a Mule Error type definition. For example, the HTTP:CONNECTIVITY error
  explained before.

- **Cause**: Expects the instance of the exception with a message for the error. For example, using
  Java, write the expression:

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

## Mock Using Then-Call

To dynamically mock a variable, use **then-call** as follows:

```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 data-value="1"></i><b>1</b></td><td>This is the processor you mock.</td></tr><tr><td><i data-value="2"></i><b>2</b></td><td>The mock calls this flow. The mock sets a variable that changes depending on the incoming event.</td></tr><tr><td><i data-value="3"></i><b>3</b></td><td>Configure the mock to call the desired flow.</td></tr></tbody></table>

## See Also

- [Assert That Event Processor](https://docs.mulesoft.com/munit/3.6/assertion-event-processor)

- [Fail Event Processor](https://docs.mulesoft.com/munit/3.6/fail-event-processor)

- [Run Custom Event Processor](https://docs.mulesoft.com/munit/3.6/run-custom-event-processor)

- [Set Event Processor](https://docs.mulesoft.com/munit/3.6/set-event-processor)

- [Verify Event Processor](https://docs.mulesoft.com/munit/3.6/verify-event-processor)
