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.
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.
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.
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.
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.
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.
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.
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.