MUnit Test Recorder
We all know MUnit plays a pivotal role in testing the Mulesoft flows/sub flow. However, MUnits are not that easy to write. It takes considerable amount of your time. Also, if you change anything in your flow/sub flow, you will need to update the Munits to make it work. MuleSoft has recently introduced an automated way to create MUnits, called MUnit Test recorder. This tutorial will explain you the steps to create MUnits using the Test recorder feature in no time.
The official documentation from MuleSoft can be read from https://docs.mulesoft.com/munit/2.2/test-recorder
Prerequisite– Anypoint Studio version- 7.5, Mule runtime version – 4.3, MUnit version 2.2.5.
In this tutorial we will create MUnit for one of HR employee API which retrieves employee information from HR database.
Steps to create MUnits using Test Recorder
Right click on any flow or sub flow, which you want to create MUnit for as shown below
Mule will start the project at the backend. So, make sure connectivity to DB and other system works fine. Otherwise recorder will not start. Once the recorder is started , you would see the Test Recorder pop up with “configure Test” option as disabled.
You will need to call the API to start the recorder. Hit the API 2-3 times.
Once the recorder has recorded the request and response. The option “configure Test” will be enabled automatically.
Click on the “Configure Test” option and screen below will appear , where you can define test name and test suite file name
Click on Next, the screen below would appear where you would be able to see various message processor in the flow and option to set the input event as in the screen below.
Click on Next message processor shown in the left tab, various option would appear at the right to mock, spy, verify the message processor. Click on the payload and you would see, mulesoft has recorded the actual payload returned by the flow.
Since we want to mock the flow, click on mock option and return the Payload should be checked (it’s enabled by default.)
Click on next message processor. Since we don’t want to do anything for this message processor , we will leave it as default “do not perform any test action”
Click on Next and the summary would appear as below. click on Finish to generate the Test.
And here we have generated the MUnit in no time.
Have a look at the src/test/resources folder. MuleSoft would have saved all the payload during the recording process inside the src/test/folder.
Run the Test and it works perfectly fine.
And here we are done with the tutorial.