Munit: Automate Munit Test Case Creation Using Munit Test Recorder

Jyoti Nimbalkar
Another Integration Blog
4 min readFeb 27, 2023

After reading this blog you will be able to,
- Create MUnit test cases using MUnit Test Recorder.
- Run autogenerated MUnit test cases and generate the coverage report.

Why MUnit?

MUnit is a Mule application testing framework that allows us to easily build automated tests for our integrations and APIs. MuleSoft provides us a full suite of integration and unit test capabilities.

MUnit plays very important role in testing the MuleSoft flows. However, writing MUnit test cases takes considerable amount of time. In future whenever mule flows are updated, MUnit test cases also must be updated.

MUnit Test Recorder in Studio

To overcome this situation, MuleSoft has introduced MUnit Test Recorder (Anypoint Studio 7.5.0 onwards) that enables us to record a processing flow and then configure a unit test based on the captured event. The test recorder captures real data as it goes through our application in Anypoint Studio. By capturing the flow execution.

In this blog we will learn simple steps to create and execute MUnit Test Recorder.

Product versions required to use the MUnit test recorder

Demonstration of creating a Test Recorder for an API

In this tutorial we will create a MUnit test recorder for a simple API which accepts application/json payload as an input, validates the payload, and converts it into application/xml.

Step 1. Create an API and run it to make sure that you are receiving expected response.

Step 2. Right click on any flow, for which you want to create MUnit for as shown below.

Step 3. Mule will start the project at the backend. Once the project is up, test recorder pops up with the message “your mule application is now running and waiting for the flow execution to start” with “Configure test” option as disabled.

Step 4. Send the request to application through postman. Now the “Configure Test” option is enabled.

Step 5. Click on “Configure test”. The flow from your Mule application is displayed in the flow Outline panel. Select a processor and apply test actions to it.

you will observe an automatically generated test summary for your application which contains the details of which component is used in each section.

Step 6. Click on Finish. You can now have a look at the automatically generated test flow under src/test/munit. Right click on the flow and run MUnit test.

You will observe that the MUnit test getting executed successfully with 100% coverage.

Have a look at the src/test/resources folder. MuleSoft has saved all the payload during the recording process.

— — — — — — — — Tutorial Ends Here — — — — — — — — — — -

MUnit test Recorder is one of MuleSoft’s out of the box solutions that helps developers save time. However, there are some limitations associated with this feature.

  • You cannot create tests for flows with Mule errors raised inside the flow, or already existing in the incoming event, even if they are handled by an on error continue error handler.
  • A recorded flow execution ends successfully, but the result does not reach its destination because the application is killed.
  • Your validations fail every time your test runs if you configure Spy or Assert processors to assert values for random data, time-dependent information (such as timestamps), or values resulting from parallel processes, because those values change in every execution.
  • Mocking values resulting from parallel processes causes a mixture of real and mocked data that compromises the execution of the processors that follow in your test.
  • Although the recorder supports data iteration in the flow, such as recursions or loops, it does not support cases in which the structure of the data being tested changes inside the iteration.
  • The recorder does not support mocking a message before or inside a Foreach processor.

--

--

Jyoti Nimbalkar
Another Integration Blog

MuleSoft Ambassador | Meetup Leader|Certified MuleSoft Developer and Architect | Staff Engineer @Nagarro| https://www.linkedin.com/in/jyoti-nimbalkar-868656154/