Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MUnit With Mule 4.0

DZone's Guide to

MUnit With Mule 4.0

This article will describe how to use MUnit 2.0 to create a test case for an application in Mule 4.0.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

This article will describe how to use MUnit 2.0 to create a test case for an application in Mule 4.0. MUnit is a mule application testing framework that allows you to easily build automated tests for your integrations and API's.

In the below application flow, the records are fetched from the MySQL database. Steps to be considered are as follows:

Image title

  • HTTP listener is configured to receive the request named as Listener.

  • Database Select operation is used to fetch data from the database where the operation takes input parameters from HTTP queryParams. Below is the configuration of the database:

Image title

SQL Query Text along with Input Parameters are as follows:

Image title

In the transform message to get the response payload as STATUS:

Image title

Logger will print the payload within the Console. Now we will create an MUnit test framework for the application, shown below:

  • Right-click on Project, Go to --> New--> MUnit test. Give the name to the test and select the flow for which test case is to be created.

MUnit Differences Mule 3.8 and Mule 4.0:

Image title

Earlier, all Mule functions were in a single category, but in Mule 4.0, they are properly grouped under two categories: MUnit and MUnit Tools. Naming conventions are also changed in the Mule 4.0 version.

A flow of the MUnit test case is shown below:

Image title

Drag-and-Drop a set event operation from Mule Palette in the Execution section that is used to pass any payload and flow variables for testing. Here, we are passing variables in the flow. Click on Add property and give the Name and Value for the variable.

Image title

Now, drag and drop the Assert that under the Validation section to assert and validate the payload. Pass the payload in the expression, Is: notNullValue(), specify an error Message which needs to be given when the test case fails.

Image title

Note: Don't forget to go back to Mule flow and Modify the values to be given in the flow. Go to input parameters of the database and change the queryParams as shown:

Image title

Now run the MUnit Test flow, and we can see a green color mark in the MUnit module that indicates that the test case has passed successfully. Now, change the values for which there is no entry in the database and you will see a red color mark for the failed test case as shown:

Image title

We can see the message will show the expected value set.

Happy learning!

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:
mule 4 ,munit ,mulesoft ,integration ,mysql ,mule flow ,mule ,tutorial ,apis

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}