MUnit in Mule 4
- May 15, 2020
MuleSoft has a framework called MUnit that allows us to write automated test cases for our application programming interfaces (APIs) and integrations. It’s a perfect fit for a continuous integration/deployment environment.
Here, we have an example on how we pass the test cases in MUnit.
Part 1: Setup sample Mule application
We’lll have to create a demo project for that in AnypointStudio. Once the project is created, we’ll drag and drop an HTTP listener from the Mule palette and do its configuration.
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'll add the database module in the palette and will drag and drop the SELECT component to the flow and do its configuration.
After selecting MySQL Connection configure and adding the Maven dependency.
After adding the dependency, we’ll have to give the credentials to connect to the database Server. Here, I’m using the mudb.learn.mulesoft.com database.
After the test connection is successful, press OK. Now we have to give the query.
After that, we have to add a transform message to make the payload to json and then add a logger:
Then, we’ll have to deploy the project. Once the project is deployed, we’ll send the request in Advanced REST Client and will check the response:
After getting the 200OK, status we can check the response.
Part 2: Dynamic query
Now we’ll give the dynamic query. For that, we’ll pass the queryParam and that we’ll store in a variable. To store the queryParam, we’ll drag and drop a Set Variable before the Select component, and we’ll change the query to dynamic query.
Now we’ll deploy the project. Once the project is deployed, we’ll send the request in Advanced REST Client and check the response.
Part 3: Setup MUnit for Mule application
For the MUnit, we’ll have to select the flow. Right click on it, and then select MUnit. Then, Create MUnit for that flow.
We’ll create an MUnit test suite, which will be present in src/test/munit.
Now we have to search for the mock in the Mule palette. Drag and drop it in the behavior section of munit-test-suite flow. Select Pick processor.
After that, we’ll have to choose the component and its doc:name. Click OK. Then, we’ll create a sample.json file in the src/test/resources.
In the sample.json file, give the response we got from SELECT to Mock it. Then, we’ll give the Payload value, as this will take the sample.json data there as payload.
Now, drag and drop the Assert under the Validation section to assert and validate the payload. Will Pass the payload in the expression, Is: notNullValue().
Now, we’ll search for Set Event to set the queryParam in the Mule palette and drag and drop it before the Flow Reference. In the attribute, we’ll set the queryParam. Here, we’re giving the queryParam i.e, the transactionID.
Part 4: Run MUnit
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. In the main flow, we can see it has passed all the components.
Part 5: MUnit test reports
For generating the MUnit Test Report, click generate report.
After clicking on the Generate Report the report will be given as shown below:
— By Putul Mandal