Contact Free trial Login

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-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’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: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.

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 you might want the returned value to change over time, or to have different responses according to certain inputs.

Mocking 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 like the example below:

<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 allows 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 error 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>
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 Using Then-Call

To dynamically mock a variable, you can 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>
1 This is the processor we wish to mock.
2 This flow will be called by the mock, which sets a variable that changes depending on the incoming event.
3 Configure the mock to call the desired flow.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub