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.

MUnit in Mule1

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:

MUnit in Mule2
MUnit in Mule3

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.

MUnit in Mule4
MUnit in Mule5

After selecting MySQL Connection configure and adding the Maven dependency.

MUnit in Mule6

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.

MUnit in Mule7

After the test connection is successful, press OK. Now we have to give the query.

MUnit in Mule8

After that, we have to add a transform message to make the payload to json and then add a logger:

MUnit in Mule9

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:

MUnit in Mule10

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.

MUnit in Mule11
MUnit in Mule12

Now we’ll deploy the project. Once the project is deployed, we’ll send the request in Advanced REST Client and check the response.

MUnit in Mule13

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.

MUnit in Mule14

We’ll create an MUnit test suite, which will be present in src/test/munit.

MUnit in Mule15

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.

MUnit in Mule16

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.

MUnit in Mule17
MUnit in Mule18

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.

MUnit in Mule19
MUnit in Mule20

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

MUnit in Mule21

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.

MUnit in Mule22

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.

MUnit in Mule23
MUnit in Mule24

Part 5: MUnit test reports

For generating the MUnit Test Report, click generate report.

MUnit in Mule25

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

MUnit in Mule26

— By Putul Mandal