MUnit With Mule 4.0
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.
Join the DZone community and get the full member experience.Join For Free
Discover how you can get APIs and microservices to work at true enterprise scale.
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:
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:
SQL Query Text along with Input Parameters are as follows:
In the transform message to get the response payload as STATUS:
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:
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:
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.
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.
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:
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:
We can see the message will show the expected value set.
Opinions expressed by DZone contributors are their own.