Mock When Event Processor

The Mock When processor allows 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-attribute attributeName="method" whereValue="#['POST']"/>
       <munit-tools:payload value="#['mockPayload']"/>

You can set the processor attribute to define the processor to mock and the with-attribute element to define the attribute’s name and value. In the example above 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:payload value="#[readUrl('classpath://sample_data/mockPost.dwl')]" mediaType="application/json" encoding="UTF-8" />

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

<wsc:consume config-ref="Web_Service_Consumer_Config" operation="OrderTshirt"/>

By configuring the mock-when processor like the example below:

<munit-tools:mock-when processor="wsc:consume">
        <munit-tools:with-attribute attributeName="operation" whereValue="#['OrderTshirt']"/>

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-attribute attributeName="config-ref" whereValue="#['HTTP_Request_configuration']"/>
	  	<munit-tools:variable key="aVariable" value="#['aValue']"/>

Mocking Errors

The Mock When processor also allows 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:listener-config name="HTTP_Listener_config" >
	<http:listener-connection host="" port="8081" />

<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']" />

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-attribute attributeName="config-ref" whereValue="#['HTTP_Request_configuration']"/> (1)
          <munit-tools:error typeId="#['HTTP:CONNECTIVITY']"/> (2)

      <flow-ref name="myFlow"/> (3)

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

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.

Mocking Errors with exceptions

The Mock When processor also allows 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-attribute attributeName="config-ref" whereValue="#['HTTP_Request_configuration']"/>
    <munit-tools:error cause="#[java!org::mule::runtime::api::connection::ConnectionException::new('MyMessage')]"/>

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub
Give us your feedback!
We want to build the best documentation experience for you!
Help us improve with your feedback.
Take the survey!