Author: Putul Mandal
MuleSoft has a framework called MUnit that allows us to write automated test cases for our APIs and integrations. It’s a perfect fit for a continuous integration/deployment environment.
Here we will do an example on how we pass the test cases in MUnit.
Part 1: Setup Sample Mule Application
- We will have to create a demo project for that in AnypointStudio as shown in the image below:

- And once the project is created, we will drag and drop an HTTP Listener from the mule palette and do its configuration as shown in the image below:


- Then we will add the database module in the palette and will drag and drop the SELECT component to the flow and do its configuration as shown below in the image:


- After selecting MySQL Connection configure and adding the Maven dependency as shown below in the image:

- After adding the dependency we will have to give the credentials to connect to the databaseServer, here I am using the mudb.learn.mulesoft.com database as shown below, we will give our credentials:

- After the test connection is successful press OK. Now we have to give the query as shown below:

- And after that we have to add a transform message to make the payload to json and then add a logger as shown below in the image:

- And after that we will have to deploy the project and once the project is deployed, we will send the request in Advanced REST Client and will check the response as shown below:

- After getting the 200OK status we can check the response.
Part 2: Dynamic Query
- Now we will give the dynamic query and for that we will pass the queryParam and that we will store in a variable. So to store the queryParam we will drag and drop a Set Variable before the Select component as shown below:

- And will change the query to dynamic query as shown below:

- Now will deploy the project and once the project is deployed, will send the request in Advanced REST Client and check the response as shown below:

Part 3: Setup MUnit for Mule ApplicationÂ
- Now for the MUnit we will have to select the flow and right click on it and then select MUnit and then Create MUnit for that flow as shown below:

- This we will create an MUnit test suite, which will be present in src/test/munit as shown below:

- Now we have to Search for the Mock When that is in the mule palette. Drag and drop it in the behaviour section of munit-test-suite flow and select Pick processor as shown below in the image:

- After that we will have to choose the component and its doc:name and after that click on OK as shown below:

- After that will create a sample.json file in the src/test/resources as shown below:

- In the sample.json file give the response we got from SELECT to Mock it as shown below:

- After it in the Mock then we will give the Payload value as shown below, as this will take the sample.json data there as payload:

- Now, will drag and drop the Assert that under the Validation section to assert and validate the payload. Will Pass the payload in the expression, Is: notNullValue() as shown below:

- Now we will search for Set Event to set the queryParam in the mule palette and drag and drop it before the Flow Reference and in the attribute part will set the queryParam, here we are giving the queryParam i.e, the transactionID as shown below:

Part 4: Run MUnitÂ
- Now right-click on flow or canvas of MUnit test flow. Select Run MUnit Suite or Debug . After runningMUnit suite, it will provide test results, errors, failures (if any) and other details on the console in Anypoint Studio as shown below:

- Â And in the main flow we can see it has has passed all the components as shown below:

Part 5:  MUnit Test reports
- For generating the MUnit Test Report we will click the generate report as shown below:

- After clicking on the Generate Report the report will be given as shown below:
