Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

An Introduction to Unit Tests by Mule- MUnit

DZone's Guide to

An Introduction to Unit Tests by Mule- MUnit

MUnit is Mule's automated testing feature. Learn to build test cases in MUnit, forming a basis you can use for your testing needs.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

Mule has a nice automated testing feature called MUnit. It is actually very easy to build and develop MUnit with simple examples. Good documentation as available with the Mule documentation but I felt It is a little bit complicated for the beginner. In this blog, I am trying to create simple, step by step examples to create an MUnit test.

Here are the high-level steps:

  • Create a simple project using HTTP.
  • Create an MUnit test case with asset equals.
  • Run the test case and verify the results.
  • Understand the XML for test cases.
  • Add more test cases and verify the result.
  • Creating a Test Project

    Let's begin.

    • Open Anypoint Studio and click on File-> New -> Mule Project.

    Image title

    • Name the project demo-munit.

    Image title

    • Create a sample project as below and test it, then stop the project.

    Image title

    Here is this part of the XML:

        <flow name="demo-munitFlow">
    
            <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
    
            <set-payload value="#['Hallo World']" doc:name="Set Payload"/>
    
        </flow>

    Creating an MUnit Test Case

    We will create an MUnit test case as follows.

    • Right-click the project and select new MUnit Test as below.

    Image title


  • The initial flow should look like this one:
  • Image title

  • Add an assert Equals as shown below.
  • Image title

  • Run the test case as shown below.
  • Image title

  • You should see a screen as below – here, the highlighted text says that it is expecting “Hallo Test” but got “Hallo world.”
  • Image title

  • Now let’s just make a small change in the test case. Let’s go to Configuration file to and change the value of “Hello Test” to “Hallo World” and re-run the test case.
  • <munit:test name="Test1-demo-munitFlowTest" description="Test">
    
            <flow-ref name="demo-munitFlow" doc:name="demo-munitFlow"/>
    
            <munit:assert-on-equals expectedValue="#['Hallo World']" actualValue="#[payload]" doc:name="Assert Equals"/>
    
        </munit:test>

    Image title

    Taking a Step Forward

     In this example, we have taken a simple test case to get started. We can keep on adding a test case which can access values or variables, flows, sub-flow, inbound parameters, etc. We can add the test cases for different scenarios. All it takes is to copy a test case like above and go edit it according to your needs. You can just copy and then go back to design mode to edit, then remove and add different test cases.

    <munit:test name="Test1-demo-munitFlowTest" description="Test">
    
            <flow-ref name="demo-munitFlow" doc:name="demo-munitFlow"/>
    
            <munit:assert-on-equals expectedValue="#['Hallo World']" actualValue="#[payload]" doc:name="Assert Equals"/>
    
    </munit:test>

    The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

    Topics:
    integration ,architecture ,mule esb ,unit testing ,java ,esb

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}