Contact Us 1-800-596-4880

Mock When Event Processor

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 the with-attribute element to define the attribute name and value. In the previous example, you are defining 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 under 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 should return. It could be a payload, a variable, attributes, or even an error.

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

Mocking Errors

The Mock When processor also enables you to mock errors in your operations. For example, assume you have an HTTP requester in your flow with an On-Error scope that catches any connectivity error and returns a custom payload if the connection fails. A sample application with this scenario would look like this:

<http:request-config name="HTTP_Request_configuration">
	<http:request-connection host="myHost" 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="myFlow">
	<http:listener config-ref="HTTP_Listener_config" path="/"/>
	<http:request method="GET" config-ref="HTTP_Request_configuration" 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="new-test-suiteTest" 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_configuration']"/> (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="myFlow"/> (3)
  </munit:execution>

  <munit:validation>
    <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalToIgnoringCase('connection error')]"/> (4)
  </munit:validation>

</munit:test>
1 Mock an HTTP Requester with the configuration of the requester in your flow.
2 Configure a then-return element to throw an HTTP:CONNECTIVITY error.
This triggers the On-error scope in your application.
3 Execute the flow containing the requester.
4 Assert that the returned payload is the one you set inside the On-error scope.

It is not possible to return all known error types of the Mule application. You can only return the type of modules used by the current flow. If you try to return an error type out of the scope of the being-tested flow, you get the MULE:UNKNOWN error instead of the configured one.

Mocking Errors with Exceptions

The Mock When processor also enables you to mock errors specifying the cause (exception instance). A sample mock instantiating the cause through Dataweave would look like this:

<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:error cause="#[java!org::mule::runtime::api::connection::ConnectionException::new('MyMessage')]"/>
  </munit-tools:then-return>
</munit-tools:mock-when>