Testing Mule Applications With Mule Domain Project Using MUnit Test Suite
Akkiraju Ivaturi takes us through the many different parts of creating and testing a Mavenized Mule project.
Join the DZone community and get the full member experience.
Join For Freein this article, we will go through the following:
- creation of a simple mavenized mule project
- creation of a domain project
- creation of a munit test
- execute the tests from anypoint studio and from command prompt using maven commands
- check the code coverage of the flow
tools used for this article:
- anypoint studio version 6.0.1 for mac
creating a simple mavenized mule project:
click file ->new mule project and name your project (in the article it was named munittest) and check the option “use maven” .
then add a simple flow with http endpoint and set a payload by dragging the elements from the “mule palette” on to the “message flow” .
corresponding xml code:
<?xml version="1.0" encoding="utf-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
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">
<!-- the following configuration will be moved to domain in order for reusing
the same configuration across all the apps -->
<http:listener-config name="http_listener_configuration"
host="0.0.0.0" port="8081" doc:name="http listener configuration" />
<flow name="munittestflow">
<http:listener config-ref="http_listener_configuration" path="/" doc:name="http"/>
<set-payload value="hello world!!!" doc:name="set payload"/>
</flow>
</mule>
now run your project and you should see the project status as deployed (right click on the project and click “run as” and select “mule application with maven” ) in the console window.
now browse the endpoint http://localhost:8081/ and you should see “hello world!!!” displayed on the browser.
since this project worked successfully, let’s move the listener configuration from the munittest.xml to the domain project by creating the domain project.
creating a mule domain project:
click file->new->mule domain project and check the option “use maven” and then go the file mule-domain-config.xml file which is available in the folder “src/main/domain” .
in this article, i have named the domain project as “testdomain” .
now remove the following configuration from the flow “munnittest.xml and add it to the mule-domain-config.xml and it should now look as below:
<?xml version="1.0" encoding="utf-8"?>
<domain:mule-domain xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:domain="http://www.mulesoft.org/schema/mule/ee/domain"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemalocation="
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.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/domain http://www.mulesoft.org/schema/mule/ee/domain/current/mule-domain-ee.xsd">
<!-- configure here resource to be shared within the domain -->
<http:listener-config name="http_listener_configuration"
host="0.0.0.0" port="8081" doc:name="http listener configuration" />
</domain:mule-domain>
note: the reason for moving the configuration to domain project is to reuse the configuration in multiple projects. for example, if there are five financial projects deployed on one server and they all are related and needs to share the same host and port with some common resources, then the ideal situation is to move the shared resources to domain project and all the projects now should be configured with the domain project. otherwise, the projects will not be deployed as there will be host and port collisions with other applications.
now go back to your munittest project and double-click on the file “mule-project.xml” and set the domain as “testdomain” as below:
after this you should see the app configured with the domain project as below:
now run your project again to check if the app is compiled and deployed properly. if you see any errors, close and open the munittest project again.
check the output in the console and it should show both the domain and the munittest app is deployed as below:
creation of munit tests:
munit test studio plugin is available with the anypoint studio and you need not download explicitly. to create tests, click file ->munit and select the flow you want to test. in our case, we will be testing the “munittestflow” and so i have selected that.
click the “finish” button.
at first instance, you will see a test created with a flow reference to the “munittestflow” as below:
now click on the pom.xml file and check the xml source.
you will see that studio has added the plugin for munit and it has “runcoverage” set as true . this means when you run the munit test suite, it calculates the coverage for that flow.
<plugin>
<groupid>com.mulesoft.munit.tools</groupid>
<artifactid>munit-maven-plugin</artifactid>
<version>${munit.version}</version>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<coverage>
<runcoverage>true</runcoverage>
<formats>
<format>html</format>
</formats>
</coverage>
</configuration>
</plugin>
also, you will observe that studio has added two dependencies in the pom file. these are useful to run the munit tests.
<dependency>
<groupid>com.mulesoft.munit</groupid>
<artifactid>mule-munit-support</artifactid>
<version>${mule.munit.support.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>com.mulesoft.munit</groupid>
<artifactid>munit-runner</artifactid>
<version>${munit.version}</version>
<scope>test</scope>
</dependency>
finally, you will observe that it has added a dependency for mule domain project. if this dependency is not added, you will get errors while running the test suites. because the listener configuration is defined in the domain project and your test suite will try to run the coverage even though when you change the setting <runcoverage> in the pom to false. because the test suite will not obey this setting while running your tests. so make sure that you have added dependency for the domain project.
<dependency>
<groupid>com.mycompany</groupid>
<artifactid>testdomain</artifactid>
<version>1.0.0-snapshot</version>
<scope>test</scope>
<type>zip</type>
</dependency>
luckily this all will be done for you by the studio and you do not have to worry about it.
run your project once to verify that we did not break anything.
execute tests from studio:
right click on the message flow area of the test suite xml created. you will find the xmls in the folder “src/test/munit” in the munittest project. double click on that file and then right click and click “run munit suite” . once it completes running, it will display the results in the munit window as below:
now go back to your flow and you should see a tick mark for “set payload” indicating that it has executed the flow. here we are not asserting anything or verifying. just trying to execute the flow. clicking on the “generate report” button in the “munit” window will give us the code coverage report.
code coverage report will be displayed as below:
now let us add logic for asserting and verifying that set payload element is called once.
for this drag and drop “assert payload” and “verify call” elements on to the test and set the properties of the two elements.
set the properties for “assert payload” as below:
if the expected value and the actual value are different, then it should show the test failure message with text “message is wrong” .
set the “verify call” as below:
here we are specifying to check if “set-payload” is called 1 time . in the properties, it first checks for message processors that matches “set-payload” and then will verify how many times the processor has been called while executing the tests.
now run your test and you should see the test should pass. if you want to test the failure, change the “assert payload” expected value to some other and you can verify the test failure message.
sometimes, while performing functional tests, we do not want to connect to some components like database or queues and so we need to mock them. whenever any call is made to those elements, we specify use the mock elements instead of actual. this will help us to verify only the functionality without actually connecting to the actual dependencies.
you can add mocks on the top section under “setup” in the test elements. in our case, let’s mock the set-payload. so whenever the actual reference is made to actual element it should use our mock element and return the value that is set in mock instead of actual.
here we are specifying in the “matching processor condition” that when message processor matches set-payload and check its attribute “doc:name” has a value “set payload” . if yes, then return a message with payload “another world” instead of “hello world!!!” .
now you need to change the “assert payload” expected value to “another world” to check if the mock element has been called upon instead of the actual “set-payload” . now run the test again and you should see test must be passing. if you see any errors, then it must be because of the expected value not changed.
executing the tests from maven command line:
go to the location of the project’s root folder from the command prompt and run the command “mvn clean test” . this will run all the tests in the project. if you want to run a specific test, refer to mulesoft website for other options.
you can get the source code from github: mule project source code .
Opinions expressed by DZone contributors are their own.
Comments