Mock When Event Processor | MuleSoft Documentation
# Mock When Event Processor
Anypoint Code Builder 
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:
```
<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:
```
%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:
```
<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.
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.   
---  
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:
```
<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:
```
<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:
```
<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:
```
<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:
```
<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>
```

Mock an HTTP Requester with the configuration of the requester in your flow.  
---  
Configure a then-return element to throw an **HTTP:CONNECTIVITY** error. This triggers the On-error scope in your application.  
Execute the flow containing the requester.  
Assert that the returned payload is the one you set in the On-error scope.  
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::lang::Exception::new("Exception Message")]
```

## Mock Using Then-Call
To dynamically mock a variable, use **then-call** as follows:
```
<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>
```
This is the processor you mock.  
---  
The mock calls this flow. The mock sets a variable that changes depending on the incoming event.  
Configure the mock to call the desired flow.  
## See Also
  * Assert That Event Processor
  * Fail Event Processor
  * Run Custom Event Processor
  * Set Event Processor
  * Verify Event Processor