Setup quickly MUnits for your project with test recorder

Last Updated on 30/03/2020 by Patryk Bandurski

Everyone knows someone that does not like writing unit tests for their code. Maybe you belong to that group. I, on the other hand, prefer to write a test to ensure that my code is as robust as possible. Of course, that does not mean that I have no errors ;). However, I am more certain of my code. MuleSoft is testing some awesome feature – test recorder. Let’s see how it works and what it gives us.


According to the MuleSoft documentation, MUnit is a testing framework that allows you to easily build automated tests for your applications. Those of you who are familiar with JUnit should see a lot of similarities.

Test scaffolding

The feature that was already available since MUnit 1 is creating a blank test for a selected (sub)flow. You need to select flow and from the MUnit menu choose to generate a blank test. Anypoint Studio than creates a munit file if it wasn’t already in place and adds an empty Test processor like in the example below.

Empty Test scope generated for selected flow
Empty Test scope generated for selected flow

Nice feature, but I do not use it often. Most of the time I create test by myself. I guess that the next feature is more likely to convince you to use MUnit tests.

Test recording

You can find some details regarding the test recording in the MuleSoft documentation, but let’s recap what gives us this tool.

  • Record input payload, variables, attributes
  • Record output payload, variables, and attributes
  • Record payload, variables, attributes for each
  • Extract recorded data in separate files in src/test/resources
  • Generate fully functioning test
  • Enable mocking external call

Let’s run test recording

In the first place, you need to select a flow/sub-flow and pick Record test for this flow from the MUnit submenu. Under the hood, Anypoint Studio runs your project and records the traffic to a specified flow.

In some cases, you may don’t do anything, as the logic is invoked for example using Scheduler. In other cases, when there is expected some event like an HTTP request you need to perform triggering action. After that, when the recording is finished New Recorded Test Wizard is presented. Below you can see the first screen.

Test recorded wizard - test name
Test recorded wizard – test name

On the next wizard page, we see all the recorded configuration. Select the name of the flow to see recorded input and output data. We can decide, for both, if we want to use payload, attributes, and variables. As in the screen below. I have decided to use just payload and variables for the input to the test.

Input is converted to Set Event element that specifies input test data. The output is converted into Assert expression that validates if the actual test execution returns the same values.

MUnit Test input and output data
MUnit Test input and output data

As you can see on the left-hand side under the flow name you have all event processors that flow has. Each can be configured separately. What can we do?

  • do nothing – default action
  • mock – for example HTTP requestor
  • verify if the event processors were called or not
  • spy what was before and after the event processors was called

When we decide to mock event processor we need to specify what data we would like to return by the mock component. I have decided to return only payload and attributes.

Mock executed event processors
Mock executed event processors

On the last wizard page, you can see the summary. After you submit the wizard Anypoint Studio generates the test. Like in the screenshot below.

Generated test based on the recording session
Generated test based on the recording session


I must say that this is a big step forward in case of writing your own MUnit tests. We spent a lot of time debugging, running our applications locally. We can now use this time to also generate test cases with almost no extra time. I have used it already a couple of times and it is a big time saver. Of course, that does not exclude the necessity to write manually some test. However, we have a good starting point. What a convenient tool! It is still in the beta phase but I keep my fingers crossed for that project.

Setup quickly MUnits for your project with test recorder

7 thoughts on “Setup quickly MUnits for your project with test recorder

  1. Somehow i tried in 4.3 version and 7.5 studio.For the first time i have clicked on record Munits. It gave some failure that some logger component thing. But after that I am seeing a disabled icon with stop recording even though i close and open project. what might be the issue?

    1. Yes, I have seen this. As you said the restart is helping in this situation. This is pretty new feature and some bugs still exist. Cheers Mate

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top