MUnit Test Cases for Mule Flows With VM Connectors

Author: Abhishek Bathwal

The MUnit Test cases for flows with VM Connectors are created with extra configurations as compared to the Normal flows MUnit Test cases.

This blog will help you in writing MUnit for such flows which implement VM Connectors.

VM Connector Flows:

We have created flows using the Publish and Listener VM Connectors.

Note:

1. Publish-Listener are async in nature (fire and forget).

2. VM intake only payload as an Input.

3. The same solution will work for Publish-Consume VM Connector.

4. To know more about VM Connectors, click here.

Flows:
VM Config:
Publish:
Listener:
DataWeave:
%dw 2.0
output application/json
---
{
	Message: "This is to test " ++ payload.Message
}

Testing Using Postman:
Anypoint Studio Console:
MUnit Test Cases:

Case1: The ideal solution is to create individual test suites for each flow.

Case2: We want to create a single test suite that includes all the related flows to verify end-to-end functionality.

MUnit Test Suite:

We need to make a small configuration in the Mock when using the MUnit tool for VM Publish. We have to provide the flow name in the Then Call section in order to include it in the test scenario.

Once we execute the test cases, it will cover all the required flows and will generate a coverage report.

Flows after MUnit Test:
Coverage Report:
Sample Flow Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="8ec75816-367d-4290-8623-31d1f340a9b7" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<vm:config name="VM_Config" doc:name="VM Config" doc:id="5a925b16-1650-4f39-be69-cb1806116b6a" >
		<vm:queues >
			<vm:queue queueName="munit" />
		</vm:queues>
	</vm:config>
	<flow name="blogFlow" doc:id="7880af40-a751-4dff-bf6b-2cf779f95b60" >
		<http:listener doc:name="Listener" doc:id="9b91ed96-b46e-479c-a8c7-ad0707a9a15d" config-ref="HTTP_Listener_config" path="/vm"/>
		<vm:publish doc:name="Publish" doc:id="e5d4b269-334c-4174-b096-91e84668f321" config-ref="VM_Config" queueName="munit"/>
	</flow>
	<flow name="blogFlow1" doc:id="b47941f7-fa4b-4cf2-b8ab-a317a85eee93" >
		<vm:listener doc:name="Listener" doc:id="5df383ae-63ab-4ab4-883d-f8877e4762fb" config-ref="VM_Config" queueName="munit"/>
		<ee:transform doc:name="Transform Message" doc:id="42f17e6c-34a7-41fc-a116-6b335f1f1505" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	Message: "This is to test " ++ payload.Message
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Logger" doc:id="bff74c7a-b757-4ba1-8542-0009889a9abb" message="#[payload]"/>
	</flow>
</mule>

Sample MUnit Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xsi:schemaLocation="
		http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
		http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
		http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">
	<munit:config name="blog-suite.xml" />
	<munit:test name="blogFlow-test" doc:id="5ecc055b-a31e-4680-99fb-b2eb26087ed4" >
		<munit:behavior>
			<munit-tools:mock-when doc:name="Mock when  VM Publish" doc:id="6433f319-73dd-4257-9706-88543bf5f37c" processor="vm:publish">
				<munit-tools:with-attributes >
					<munit-tools:with-attribute whereValue="Publish" attributeName="doc:name" />
					<munit-tools:with-attribute whereValue="e5d4b269-334c-4174-b096-91e84668f321" attributeName="doc:id" />
				</munit-tools:with-attributes>
				<munit-tools:then-call flow="blogFlow1" />
			</munit-tools:mock-when>
		</munit:behavior>
		<munit:execution >
			<munit:set-event doc:name="Set Input" doc:id="008cb1d9-1b99-420a-b308-35368200c1ff">
				<munit:payload value='#[output application/json&#10;---&#10;{&#10;    "Message": "Munit for VM"&#10;}]' encoding="UTF-8" mediaType="application/json " />
				<munit:attributes value="#[readUrl('classpath://blogFlowtest\set-event_attributes.dwl')]" />
			</munit:set-event>
			<flow-ref doc:name="Flow-ref to blogFlow" doc:id="b53f5998-a391-48f5-8f6b-6a2d44b58f19" name="blogFlow"/>
		</munit:execution>
		<munit:validation>
			<munit-tools:assert-equals doc:name="Assert equals" doc:id="fab1a73d-7cb8-4a42-b6d7-db078891294e" actual="#[payload]" expected='#[{&#10;    "Message": "This is to test Munit for VM"&#10;}]'/>
		</munit:validation>
	</munit:test>
</mule>

Happy Learning!

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.