Continuation of my previous blog about MUnit Part-1 and Part-2 this is the last part of blog about MUnit, so if you have not read previous parts yet I will highly recommend you to go thorough it before continuing
MUnit – Test your mule flow – Part 1
Munit test your mule flow- part-2
or you can watch the video tutorial at youtube
Part 1 Video – https://www.youtube.com/watch?v=on7uQpWXV1E&feature=youtu.be
Part 2 Video – https://youtu.be/KrF8K8IDJuY
Unit Test Case 3 -> main-flow-test-suite.xml
Unit testing main flow is bit more complicated, since in a unit test case we should only be testing the flow under test and not the flow reference to other flow, but since our main flow has a flow reference what we need to do is use MUnit mocking framework and mock the flow reference before we call the main flow. Once the mock object is ready MUnit will replace the flow reference call with the mock object and test our flow.
By doing mocking we achieve the autonomous implementation for our unit test case and we can run our test case even if the flow reference is not available or in case the referenced flow is still not implemented.
Let’s see how to do that
<munit:test name="doc-test-mainFlow-unit-Test_1" description="Unit Test case asserting scenario 1"> <mock:when messageProcessor="mule:set-payload" doc:name="Mock"> <mock:with-attributes> <mock:with-attribute whereValue="#['Set Payload']" name="doc:name" /> </mock:with-attributes> <mock:then-return payload="#[]" /> </mock:when> <mock:when messageProcessor="mule:flow" doc:name="Mock"> <mock:with-attributes> <mock:with-attribute whereValue="#['decisionFlow']" name="name" /> </mock:with-attributes> <mock:then-return payload="#[]"> <mock:invocation-properties> <mock:invocation-property key="decisionVariable" value="#['decision1']" /> </mock:invocation-properties> </mock:then-return> </mock:when> <flow-ref name="munit-demoFlow" doc:name="Flow-ref to main flow" /> <munit:assert-payload-equals message="oops, wrong payload!" expectedValue="#['Decision 1 was taken']" doc:name="Assert Payload" /> </munit:test>

In the above test case and the image you can see we have two mocking objects
- we are mocking the set-payload and setting an empty payload, we need to do this because the flow reference i.e. decision flow is expecting a payload for it’s execution and before we go and mock the flow reference we need to satisfy the input values for this flow
- In the second mock object we are mocking the flow which is quite simple, as you can see that we have given the whereValue attribute and put the name of decisionFlow and told the mock framework that this name is the name of flow, now how does framework knows that it has to mock a flow, it is done by the messageProcessor property where we give that the object that we want to mock is mule:flow
- another important thing to notice is that we are also setting invocation-property before coming out of flow mock, this is required because our main flow needs a flow variable named decisionVariable to take the decision and set the payload
Again I have left the implementation of second flow test for you to try.
Functional Test Case -> functional-test-suite.xml
The functional test is very simple in our case, so basically in functional test case we will be testing end-to-end flow of our application, since our flow starts from the HTTP endpoint so we will not be calling HTTP endpoint instead we will be calling flow with its name as a flow reference and setting a inbound-property decisionKey in http query params and doing two test case to test both the outputs
<munit:test name="functional-test-1-munit-demoFlowTest" description="Test"> <munit:set payload="#['']" doc:name="Set Message"> <munit:inbound-properties> <munit:inbound-property key="http.query.params" value="#[['decisionKey':'value1']]" /> </munit:inbound-properties> </munit:set> <flow-ref name="munit-demoFlow" doc:name="Flow-ref to munit-demoFlow" /> <munit:assert-payload-equals message="Sorry wrong payload" expectedValue="#['Decision 1 was taken']" doc:name="Assert Payload" /> </munit:test>
I think the code above is self-explanatory so I will leave it up to you to understand
Video Tutorial
With this we come to end of tutorial about MUnit, do let me know if it was of any help for you.
I hope you liked the blog, please like and share and subscribe my youtube channel for latest technical blogs.