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

  1. We will have to create a demo project for that in AnypointStudio as shown in the image below:
  1. 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:
  1. 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:
  1. After selecting MySQL Connection configure and adding the Maven dependency as shown below in the image:
  1. 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:
  1. After the test connection is successful press ok.Now we have to give the query as shown below for it as shown below in the image:
  1. 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:
  1. And after that we will have to deploy the project and once the project is deployed, will send the request in Advanced REST Client and will check the response as shown below :
  1. After getting the 200OK status we can check the response.

Part 2: Dynamic Query

  1. Now 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 will drag and drop a Set Variable before the Select component as shown below:
  1. And will change the query to dynamic query as shown below:
  1. Now will deploy the project and once the project is deployed, will send the request in Advanced REST Client and will check the response as shown below:

Part 3: Setup MUnit for Mule Application 

  1. 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:
  1. This will create an MUnit test suite, which will be present in src/test/munit as shown below:
  1. 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:
  1. After that we will have to choose the component and its doc:name and after that OK as shown below:
  1. After that will create a sample.json file in the src/test/resources as shown below:
  1. In the sample.json file give the response we got from SELECT to Mock it as shown below:
  1. After it in the Mock When will  give the Payload value as shown below as this will take the sample.json data there as payload:
  1. 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:
  1. 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 

  1. 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:
  1.  And in the main flow we can see it has has passed all the components as shown below:

Part 5:  MUnit Test  reports

  1. For generating the MUnit Test Report we will click the generate report as shown below:
  1. After clicking on the Generate Report the report will be given as shown below:

Leave a Comment

You must be logged in to post a comment.