An example of how to create an MUnit2 test

04.06.2018

This example takes you through the process of creating a series of MUnit2 tests, which are aimed at validating the behaviour of a simple code sample.

Sample Production Code

The sample code for this example is simple, but it uses some of Mule’s most common message processors. It implements a basic use case:

  1. It receives an HTTP request.
  2. It extracts data from the request to route a message through the application.
  3. It decides how to create a response.

 MUnit mainflow

 MUnit secondaryflow

 MUnit- SubFlow

<?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:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="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:listener-config name="HTTP_Listener_config"

              doc:name="HTTP Listener config">

              <http:listener-connection host="0.0.0.0"

                     port="${http.port}" />

       </http:listener-config>

       <configuration-properties file="mule-artifact.properties"

              doc:name="Configuration properties" />

       <flow name="mainFlow">

              <http:listener config-ref="HTTP_Listener_config" path="/"

                     doc:name="HTTP Listener" allowedMethods="GET" />

              <set-payload value="#[attributes.queryParams.url_key]"

                     doc:name="Set query param 'url_key' to payload" />

              <flow-ref name="secondaryFlow" doc:name="secondaryFlow" />

              <choice doc:name="Choice">

                     <when expression="#[vars.flowValue == 'flowValue_1']">

                           <set-payload value="#['responsePayload_1']" doc:name="Set Response Payload" />

                     </when>

                     <otherwise>

                           <set-payload value="#['responsePayload_2']" doc:name="Set Response Payload" />

                     </otherwise>

              </choice>

       </flow>

       <flow name="secondaryFlow">

              <choice doc:name="Choice">

                     <when expression="payload == 'payload_1'">

                           <flow-ref name="firstSubFlow" doc:name="firstSubFlow" />

                     </when>

                     <otherwise>

                           <flow-ref name="secondSubFlow" doc:name="secondSubFlow" />

                     </otherwise>

              </choice>

       </flow>

       <sub-flow name="firstSubFlow">

              <set-variable variableName="flowValue" value="flowValue_1"

                     doc:name="Set Variable" />

       </sub-flow>

       <sub-flow name="secondSubFlow" doc:id="412b39dc-41ed-4917-bf6a-1120f91d4ad5">

              <set-variable variableName="flowValue" value="flowValue_2"

                     doc:name="Set Variable" />

       </sub-flow>

</mule>

Test-Suite

 MUnit-1

 MUnit-2

 MUnit FunctionalTest mainFlow -1

MUnit FunctionalTest mainFlow -2

MUnit- test_withMock_1

MUnit- test_withMock_2

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"

       xmlns="http://www.mulesoft.org/schema/mule/core" 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.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd

              http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">

       <munit:config name="munit2-test-suite.xml" doc:name="MUnit configuration"/>

       <import file="munit2.xml" doc:name="Import" />

       <munit:test name="secondaryFlowTest_1" description="MUnit Test">

              <munit:execution >

                     <munit:set-event doc:name="Set Message payload == payload_1">

                           <munit:payload value="#['payload_1']" />

                     </munit:set-event>

                     <flow-ref name="secondaryFlow" doc:name="secondaryFlow" />

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[vars.flowValue]" is="#[MunitTools::equalTo('flowValue_1')]" doc:name="Assert payload"/>

                     <munit-tools:verify-call processor="mule:flow-ref" doc:name="Verify call"  times="1">

                           <munit-tools:with-attributes>

                                  <munit-tools:with-attribute attributeName="name" whereValue="#['firstSubFlow']" />

                           </munit-tools:with-attributes>

                     </munit-tools:verify-call>

              </munit:validation>

       </munit:test>

       <munit:test name="secondaryFlowTest_2" description="MUnit Test">

              <munit:execution >

                     <munit:set-event doc:name="Set Message payload == payload_2">

                           <munit:payload value="#['payload_2']" />

                     </munit:set-event>

                     <flow-ref name="secondaryFlow" doc:name="secondaryFlow"/>        

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[vars.flowValue]" is="#[MunitTools::equalTo('flowValue_2')]" doc:name="Assert payload"/>

                     <munit-tools:verify-call processor="mule:flow-ref" doc:name="Verify call"  times="1">

                           <munit-tools:with-attributes >

                                  <munit-tools:with-attribute attributeName="name" whereValue="#['secondSubFlow']" />

                           </munit-tools:with-attributes>

                     </munit-tools:verify-call>             

              </munit:validation>

       </munit:test>

       <munit:test name="functionalTest_mainFlow_1" description="MUnit Test">

              <munit:execution >

                     <munit:set-event doc:name="Set Query Parameter url_key=payload_1">

                           <munit:payload value="#['']" />

                           <munit:attributes value="#[{queryParams: {url_key: 'payload_1'}}]" />

                     </munit:set-event>

                     <flow-ref name="mainFlow" doc:name="mainFlow"/>

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[vars.flowValue]" is="#[MunitTools::equalTo('flowValue_1')]" doc:name="Assert payload"/>

              </munit:validation>

       </munit:test>

       <munit:test name="functionalTest_mainFlow_2" description="MUnit Test">

              <munit:execution >

                     <munit:set-event doc:name="Set Query Parameter url_key=payload_2">

                           <munit:payload value="#['']" />

                           <munit:attributes value="#[{queryParams: {url_key: 'payload_2'}}]" />

                     </munit:set-event>

                     <flow-ref name="mainFlow" doc:name="mainFlow"/>

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[vars.flowValue]" is="#[MunitTools::equalTo('flowValue_2')]" doc:name="Assert payload"/>

              </munit:validation>

       </munit:test>

       <munit:test name="test_withMock_1" description="MUnit Test">

              <munit:behavior >

                     <munit-tools:mock-when processor="mule:set-payload" doc:name="Mock when">

                           <munit-tools:with-attributes >

                                  <munit-tools:with-attribute attributeName="doc:name" whereValue="#[&quot;Set query param 'url_key' to payload&quot;]" />

                           </munit-tools:with-attributes>

                           <munit-tools:then-return >

                                  <munit-tools:payload value="#['']" />

                           </munit-tools:then-return>

                     </munit-tools:mock-when>

                     <munit-tools:mock-when processor="mule:flow-ref" doc:name="Mock when">

                           <munit-tools:with-attributes >

                                  <munit-tools:with-attribute attributeName="name" whereValue="#['secondaryFlow']" />

                           </munit-tools:with-attributes>

                           <munit-tools:then-return >

                                  <munit-tools:payload value="#['']" />

                                  <munit-tools:variables >

                                         <munit-tools:variable key="flowValue" value="#['flowValue_1']" />

                                  </munit-tools:variables>

                           </munit-tools:then-return>

                     </munit-tools:mock-when>

              </munit:behavior>

              <munit:execution >

                     <flow-ref name="mainFlow" doc:name="mainFlow"/>

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalTo('responsePayload_1')]" doc:name="Assert that"/>

              </munit:validation>

       </munit:test>

       <munit:test name="test_withMock_2" description="MUnit Test">

              <munit:behavior >

                     <munit-tools:mock-when processor="mule:set-payload" doc:name="Mock when">

                           <munit-tools:with-attributes >

                                  <munit-tools:with-attribute attributeName="doc:name" whereValue="#[&quot;Set query param 'url_key' to payload&quot;]" />

                           </munit-tools:with-attributes>

                           <munit-tools:then-return >

                                  <munit-tools:payload value="#['']" />

                           </munit-tools:then-return>

                     </munit-tools:mock-when>

                     <munit-tools:mock-when processor="mule:flow-ref" doc:name="Mock when">

                           <munit-tools:with-attributes >

                                  <munit-tools:with-attribute attributeName="name" whereValue="#['secondaryFlow']" />

                           </munit-tools:with-attributes>

                           <munit-tools:then-return >

                                  <munit-tools:payload value="#['']" />

                                  <munit-tools:variables >

                                         <munit-tools:variable key="flowValue" value="#['flowValue_2']" />

                                  </munit-tools:variables>

                           </munit-tools:then-return>

                     </munit-tools:mock-when>        

              </munit:behavior>

              <munit:execution >

                     <flow-ref name="mainFlow" doc:name="mainFlow"/>

              </munit:execution>

              <munit:validation >

                     <munit-tools:assert-that expression="#[payload]" is="#[MunitTools::equalTo('responsePayload_2')]" doc:name="Assert that"/>

              </munit:validation>

       </munit:test></mule>

 

If you would like to find out more about the MUnit2 testing framework, do give us a call at +44 (0)203 475 7980 or email us at marketing@whishworks.com

Other useful links:

MUnit short tutorial 

Mule 4 and Studio 7 – a bird’s-eye view of what’s new

API Recipes with MuleSoft Anypoint Platform

Recent Posts