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.
How we can pass the environment variable to record test for any flow.?
Hi Ruby, Hope you are doing well!
Unfortunately, there is no option to pass the environment variable as of now. Instead we added them as “Global property” to make the MUnit recording work. Here is the snippet of the xml below.
<global-property doc:name=”Global Property” doc:id=”992febd0-9aae-48d1-a298-69bf2eaf3631″ name=”env” value=”dev” />
Hi do we pass master key which is required to decrypt the values so that we can record the test of any flow.Without it recording is failing with deployment error.
Hi Rajnish, Hope you are doing well!
As of now we don’t see any option to pass the variables while recording MUNITs. However you can create global properties for all the environmental variables as below in your project. This way you will not get any error during the recording.
Let us know if you still face issue in recording MUnits.
Hi ,
I am not getting test recorder pop up, can you suggest on same.
It works only with Anypoint Studio version- 7.5, Mule runtime version – 4.3, MUnit version 2.2.5 and above.
Thanks,
Mulesy Team
Yes , I am using same.
thanks!!!
Hi It is for success scenario what about failure one
How I can record test for prc api which calls sys api for success scenario to work.
Munit recording is only working for positive scenario (happy path). would you please add one example that show us how we are going to create negative scenario.