Leveraging MUnit for Test Automation
A key CI and CD capability of any enterprise solution is a testing framework to write and run repeatable tests. MUnit has tons of test automation capabilities.
Join the DZone community and get the full member experience.
Join For FreeTraditional integration platforms could get away with providing some command line tools to automate the build and deployment of applications built on their platform. However, in the modern world, integration platforms need to encompass the critical API management and cloud components, as well, so the scope of Continuous Integration and Continuous Delivery tools are no longer just limited to integration applications only. This also requires support for provisioning integration software and applications in private or public cloud platforms and capability to automate governance of deployed applications.
A key CI and CD capability of any enterprise solution is a testing framework to write and run repeatable tests, so in this article, we will focus on the test automation capabilities of the platform provided by MUnit.
MUnit is a Mule application testing framework that allows you to easily build automated tests for your integrations and APIs. It provides a full suite of integration and unit test capabilities and is fully integrated with Maven and Surefire for integration with your continuous deployment environment.
With MUnit, you can:
- Create your Mule test by writing Mule or Java code, and disable flow inbound endpoints and endpoint connectors.
- Mock outbound endpoints and message processors.
- Spy any message processor and verify message processor calls.
- Create unit and integration tests in a local environment (MUnit allows you to start a local FTP/SFTP, DB server, or mail server).
- Call the Mule client from Mule code and assert flow exceptions.
- Enable or disable particular tests, and see assertion and error reports with Mule stack trace.
- Extend the MUnit framework with plugins.
- Check visual coverage and debug your tests with Studio.
Here is a quick example of how to test an existing application developed in another how-to series blog.
1. Download the Existing HowTo Project
Click here to download. Set up the prerequisites as instructed in the readme and exchange documentation for executing the project.
2. Import the Existing Project Into a New Workspace
Use Anypoint Studio. Save as devops-howto_1.0.0.
3. Make Sure the Project Points to JRE
It needs to point to JRE on the local machine as shown below (File > Properties > Java Build Path).
4. Install MUnit
If not already installed, install MUnit (Help > Install New Software).
5. Create a New Test Suite
Once MUnit is installed, right-click on the project and create a new test suite. The test suite is created in the src/test/munit folder.
The test flow in the new test-suite already has reference to the soap-on-db service to be invoked. Add a Set Message processor before the invocation to set the input payload as required to invoke the service. Copy the payload string from the /src/test/resources/SetMessage.xml file into the payload field as shown below.
6. Store the Response From the Web Service
Use Object to String and Set Property processors to store the response from the web service.
7. Set the Expected Response
Use the Transform Message processor. Copy the response from /src/test/resources/ExpectedSOAPResponse.xml and ensure that the response is enclosed within single quotes.
8. Store the Expected Response
Store the expected response in a property as shown below.
9. Compare the Actual and Expected Response
Use the Assert Equals message processor to compare the actual and expected response. If the responses do not match, the assertion will throw the configured exception in the generated report.
10. Select and Run the Test Suite
A test report is generated in the MUnit tab with a green bar indicating that the test executed successfully. Green checkmarks against the message processors in the web service flow indicate all processors that were tested as part of the flow.
11. Simulate an Error Report
To simulate an error report, modify the expected response as below in the Expected Response processor. Change the loan ID to 101.
The error report generated would then indicate the difference between expected and actual response, marking the test as a failure.
To save time, you can also download the preconfigured MUnit project from Anypoint Exchange and execute the above tests.
To summarize, MUnit is fully integrated with Anypoint Studio and provides a simple easy-to-use graphical interface to develop, debug, and check coverage of test cases. It provides a full suite of unit and integration test capabilities and is fully integrated with Maven and Surefire ensuring comprehensive support for DevOps CI/CD processes. For a complete list and details around MUnit's capabilities, refer to the MUnit documentation.
Published at DZone with permission of Prashant Joshi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments