Mulesoft Unit Testing in Mule 3 – step by step tutorial

Overview

 

Unit testing is a method used to ensure that units of the written code are working as expected (design and behavior).

Mulesoft uses Munit, a native testing framework for Mule. Munit is fully integrated with Maven for integration with your continuous deployment environment.

In this tutorial we are using Anypoint studio version 6.6.4, and we will show you how to create, run, and review the result of a unit test In Anypoint Studio.

Summary:

 

Steps

 

1- Install Munit

 

1.1- Open Anypoint Studio

  • select Help -> Install new software.
  • In the search bar write Munit and select Munit and Munit Tools for Mulesoft.
  • Next -> Finish

 

1.2- Go to Anypoint Studio Package Explorer:

  • Right-click in your project>Munit>Configure Munit Maven Support
  • Right-click in your project>Munit>Add Munit Library to classpath

These steps add the Munit dependencies to the project POM.

 

2- Create Munit Suite

 

2.1- Go to Anypoint Studio Package Explorer:

  • Right-click in your project -> New -> Munit test
  • Choose the flow/flows you want to test
  • The  Munit suite is located under src/test/munit

If you need to add a flow after this you can right-click on the flow -> Munit -> Add Test to suite -> (the suite you created)

 

3- Creating the Unit tests

 

There are multiple test situations, depending on the flow that you need to test.

For this tutorial we will use an example that can be used in most test situations.

To have a successful unit test you have to mock every external call, this means that we are only testing the Anypoint implementation.

Let’s take a look and evaluate the example flow:

After evaluating the flow we can see that we will need to have a mock response of the HTTP connector.

In the Munit test suite there will be a flow with a Flow Reference for each flow that you selected to test.

3.1-If the flow has a request body, query parameters or other inbound parameters you have to use Set Message Connector

3.2 Mock external Response:

  • Use .*:.* to match all
  • Set the attribute name as doc:name (looks for this flag on the xml)
  • Set the Value with the name of the connector you want to mock (in the example case is the HTTP connector with the name HTTP)
  • Create a file under  src/test/resources with the response payload you need for the mock
  • Set the return message (set the message path)

To set the return message path you need to point to the file name. To do this you need to use:

  • getResource -> points to the src/test/resources folder
  • file name and extension -> points to the specific file
  • conversion -> if needed in the response (example: asString())

 

If you need to check the input and output of the event you can use the spy connector

For this use the same attributes as the mock configuration.

The last step in the creation of a unit test is the validation of the response.

There are multiple ways of validation of the response from the integration, for this, you have multiple connectors available.

For the tutorial purpose we will check if the payload is not null.

 

4-Running the Unit Test

 

4.1- Run the unit test:

  • right click on the test suite
  • Run Munit Test

After running the test you can see on the Munit separator if the test was successful or not (list of errors and failures)

If you want to test only one or a group of flows, you can ignore the ones you don’t want to test. (select the flow -> right-click -> ignore test)

 

5-Test Coverage

 

To see the test Coverage, under the Munit separator select Coverage> Generate Report, and a new tab will appear with the coverage report.

 

Leave a Reply

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