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

MUnit Test Recorder

 

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.

MUnit Test Recorder

 

You will need to call the API to start the recorder. Hit the API 2-3 times.

MUnit Test Recorder

 

Once the recorder has recorded the request and response. The option “configure Test” will be enabled automatically.

MUnit Test Recorder

 

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.

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ruby
4 years ago

How we can pass the environment variable to record test for any flow.?

Rajnish
Rajnish
4 years ago

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.

Apeksha Jain
Apeksha Jain
3 years ago

Hi ,
I am not getting test recorder pop up, can you suggest on same.

Apeksha Jain
Apeksha Jain
3 years ago
Reply to  admin

Yes , I am using same.
thanks!!!

ramakrishna
ramakrishna
2 years ago

Hi It is for success scenario what about failure one

Amit G
Amit G
2 years ago

How I can record test for prc api which calls sys api for success scenario to work.

Abiy N Ayalew
Abiy N Ayalew
6 months ago

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.