
Today we will be talking about testing in Mule ESB applications, In any kind of technological development testing plays a major part. With testing you make sure that the code you have written can withstand the rough environment of production. Testing can be of various types depending on your specific needs i.e. the type of product or application you are involved in developing.
Since we are talking about Mule and as a mule developer there are two main types of testing you should be interested
-
Unit Testing
-
Integration Testing
Unit Testing
As the name suggests unit testing is a process by which you test the smallest unit of your source code. What you call a smallest unit can vary according to the programming principals you are working on, but it always concentrate on validating the correctness of an individual unit of code.
In Mule application, we consider smallest testable part as Mule flow( sub-flow) so this is our unit of code.
A good Unit test should be independent of others, why? because if it depends on other component and during testing we get fail result it becomes difficult to judge which component has failed. To avoid mistakes of failure of other component as failure of unit under test always make your unit test independent.
To isolate unit test case in Mule we can use mock messages, we will talk more about it later.
Integration Testing
An application is built by collaboration of smaller units, in Unit test you test the smaller units and in Integration Testing you test how these units of code respond when collaborating between each other. The intention is to validate how different units of code/modules work together.
Coding for Testing
- Write short flows– If your code is vary long then it is very hard to unit test it, try to avoid long flows, rule of thumb is if you have to scroll your screen to see the flow it is too long try and divide it into sub-flows.
- Modularize – As you don’t write all your code in single class file similarly don’t put all your flows in single file. Best way is to group them based on common goals or functional area
- Environmentalism – Parameterise your code to work on different environments like DEV TEST PROD etc. using placeholders for example the settings of DB or outbound HTTP endpoints.
- Readability and Maintainability – Writ your test like you write your code to be readable and maintainable, give proper names to your tests.
- Error Message – Always give proper error messages to you your test cases a wrongly written or ambiguous is worse than doing no testing.
MUnit
MUnit is a Mule testing framework that lets you easily automate testing Mule applications, using both unit and integration tests. MUnit also provides a set of tools, such as a message processor mocking framework that lets you test units of code in actual isolation.
Demo of MUnit
In this part of blog I will be explaining the use case for which we will be writing MUnit test cases and in the next part of blog I will show how to write and run those test cases in Anypoint Studio.
The use case that I am going to cover is as below,
Main Flow
we have a main flow which is using http inbound endpoint and extracts the information from the http-params sent to the inbound, Then it calls another flow and sends the information as payload, our main flow has a choice which depends upon the variable set by decision flow and sub-flow.

Decision Flow
The decision flow uses the payload sent by the main flow and takes the decision of calling the two sub-flows, these sub-flows will simply set an variable with some value.


Complete XML
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="munit-demoFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/mutest" doc:name="HTTP"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.['decisionKey']]" doc:name="Set Payload"/>
<flow-ref name="decisionFlow" doc:name="decisionFlow"/>
<choice doc:name="Choice">
<when expression="#[flowVars.decisionVariable.equals('decision1')]">
<set-payload value="#['Decision 1 was taken']" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="#[#['Decision 2 was taken']]" doc:name="Set Payload"/>
</otherwise>
</choice>
</flow>
<flow name="decisionFlow">
<choice doc:name="Choice">
<when expression="#['value1'.equals(payload)]">
<flow-ref name="decision1SubFlow" doc:name="Flow Reference 1"/>
</when>
<otherwise>
<flow-ref name="decision2SubFlow" doc:name="Flow Reference 2"/>
</otherwise>
</choice>
</flow>
<sub-flow name="decision1SubFlow">
<set-variable variableName="decisionVariable" value="#['decision1']" doc:name="Decision Variable 1"/>
</sub-flow>
<sub-flow name="decision2SubFlow">
<set-variable variableName="decisionVariable" value="#['decision2']" doc:name="Decision Variable 2"/>
</sub-flow>
</mule>
Testing Flow using Postman
We will call our flow using postman and send decisionKey as query param with two different values and will see the response.


As we can see the our use case runs properly, Now in second part of this blog series I will be showing how to write MUnit test cases to test our various flows.
Video Tutorial
I have created the first part of MUnit tutorial to set up the application to be tested using MUnit, you can see the video here
Please subscribe my youtube channel to get early notifications of new tutorials.
Do let me know your thoughts about this blog, Please comment, share and follow my website to give me motivation to keep posting good articles.
