Author: Abhishek Bathwal
The MUnit test cases for flows with the DataWeave Lookup function 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 use DataWeave Lookup function.
Note: To know more about DataWeave Lookup function, click here.
Flows:

DataWeave:
%dw 2.0
output application/json
---
lookup("lookupFlow1", payload)
DataWeave in lookupFlow1
%dw 2.0
output application/json
---
{
Message: "This is to test " ++ payload.Message
}
Testing Using Postman:

MUnit Test Cases:
MUnit Test Suite:

Error MUnit Test:

Reason:
MUnit cannot detect the reference of other flow in the DataWeave Lookup function.
Solution:
In order to solve the above error, we need to do a small configuration in the Test Suite. We have to provide the flow name in the Enable Flow Sources (List of flows) section in order to allow the Lookup function to call the respective flow.Â

Once we execute the test cases, it will cover all the required flows and generate a coverage report.Â
Flow after Success MUnit Test:

Coverage Report:

Sample Flow Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="d3552853-e9bf-479a-b032-e6f9f23d654f" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="lookupFlow" doc:id="d5c34bf3-014a-44b6-949d-fdf56cbcc1ae" >
<http:listener doc:name="Listener" doc:id="5e6dc296-b9d5-4226-a239-b341e0649a38" config-ref="HTTP_Listener_config" path="/lookup"/>
<logger level="INFO" doc:name="Started" doc:id="cf1a83f4-955d-453c-8cf6-46ca0f3cc2a9" message="Start Lookup Flow"/>
<ee:transform doc:name="lookup call" doc:id="4893879c-ae44-4b5d-825f-8f419d51ca72" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
lookup("lookupFlow1", payload)]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Completed" doc:id="ff8a85d8-2940-4f4b-988c-070eee5a1d09" message="End Lookup Flow"/>
</flow>
<flow name="lookupFlow1" doc:id="1277bddc-477d-4173-a331-f0c73a6f6135" >
<ee:transform doc:name="Transform Message" doc:id="e7200686-f112-47da-85e0-9254c8eb34ef" >
<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="Response" doc:id="aec98287-eb7f-41c0-87a3-8aac17f5c597" 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="lookup-test-suite.xml" />
<munit:test name="lookup-test-suite-lookupFlowTest" doc:id="77731004-88c7-47bc-bae4-de16e27749dd" description="Test">
<munit:enable-flow-sources >
<munit:enable-flow-source value="lookupFlow1" />
</munit:enable-flow-sources>
<munit:execution >
<munit:set-event doc:name="Set Event" doc:id="4de86904-fc62-45db-af9d-7d4198401552" >
<munit:payload value='#[output application/json --- { "Message": "Dataweave Lookup Function" }]' />
</munit:set-event>
<flow-ref doc:name="Flow-ref to lookupFlow" doc:id="2b52284b-6bc3-452e-995d-3fe3f9faf682" name="lookupFlow"/>
</munit:execution>
<munit:validation >
<munit-tools:assert-equals doc:name="Assert equals" doc:id="3feb5a7c-a39c-4b9b-ab81-84f51c45a335" message="Output not Matched" actual="#[payload]" expected='#[{ "Message": "This is to test Dataweave Lookup Function" }]'/>
</munit:validation>
</munit:test>
</mule>
Happy Learning!