Mule Integration Framework facilitates unit testing of mule applications through writing test cases leveraging the power of its native testing framework: MUnit. In the world of software development, unit testing plays a vital role on validating the correctness of a piece of code so that the potential problems and root causes can be isolated at an early phase and can be fixed before the code breaks any dependent code in release or in production environment.
This article encompasses the idea of writing MUnit test case in the test-driven development environment of Mule along with recommendations on what we should consider while writing a good test case.
Scenario: The underlying concept here is to capture a change requirement scenario. Following is the target flow – as part of some change requirement the dataweave transformation of the very first transform message component has been changed. In the existing project, there is a MUnit test case to test the main flow. Hence, to validate the outcome of modified transformation, it is wise to create a separate unit test case only for the transformed component with positive/negative cases.
Brief description of the test suit to validate Transformation output:
Here no flow-ref has been used to invoke the sub-flow since our scope is only to verify the transformation output.
To initialize the test suit each MUnit test file must contain the following:
- An import section (defines the files needed to run the test suit)
- At the beginning of the test case, the Set Message processor is used to locate the payload that has to be passed through the transformation. The location of the file is /example folder under src/test/resources and The MIME type is mentioned as text/json to reflect the original payload type.
- The next two message processors are used to convert the original payload to the form as it is received by the target transform message component in the original sub flow. The set variables transformer is used to pre-set some dependent variable values that have been used in the transformation of payload.
- Payload Transform is the transform message component in this case which refers to the original dw file containing the transformation of the payload. Here, it is important to refer to the original dw file rather than copying the transformation body so that the test case will ensure the validation of further changes in the same transformation in future. Build Actual and Expected Values: this has been used to create two variables to contain the expected response and actual response from the transformation that occured immediately before this. Here again, the expected response is kept in a xml file under the location: src/test/resources.
- Finally, Assert component is used to check whether the actual output matched with the expected output.
Considerations for simple and efficient MUnit test case:
- A good MUnit test case must be simple and target oriented ie. it should only focus to validate a specific function or processor of a flow. If there are multiple optional paths present in a flow, separate test cases should be created to validate each of them.
- While writing a test case, we should isolate our code block. The code should be independent of any external systems or applications. If the code is dependent on some other part of the code it is suggested to mock that part.
- Maintaining meaningful naming convention aligned with the target flow and functionality is another important aspect. If request/response example files are used, the location and name should be purposeful as well; this will lend to its readability
- Writing MUnit test cases becomes easier when the target codes are properly modularized and structured. Hence, it is of important to take into consideration the test cases while writing the code which will eventually bring simplicity and more manageability in generating test cases.
Incepta is a certified MuleSoft partner with an experienced team of experts in MuleSoft development and consulting. Visit our website: www.inceptasolutions.com or email us at email@example.com to learn more.
About Incepta Solutions
At Incepta Solutions, our team of #InceptaInnovators is passionate about developing the bridge between people and operations, as we create stories that we can all be proud of.
Since our inception in 2010, we are recognized as trusted experts in providing digital services to global businesses. We are proud to be named one of the top 5 Information Technology companies in Canada on the Growth List 2020 (published by Canadian Business and MacLeans). In addition, we are humbled to be recently certified as a Great Place to Work.
Our full suite of digital services includes:
Cybersecurity | Integration | Digital Transformation
Data Management | Cloud Strategy | Customer 360
At Incepta Solutions, we provide business solutions that solve challenges and enable future growth and success for all of our clients. We leverage industry-leading technologies to provide innovative solutions that are robust, of premier quality, and cost-effective.
In our growth journey, our goal is to become a global leader in digital transformation and enterprise solutions. We enable businesses all over the world to solve complex and critical integration challenges. As #InceptaInnovators, we hope to look back at these times on how we have helped global brands and enterprises achieve success. Our driving force is that one day, we are able to reflect on our shared journey and be proud of the stories we have created together.