Sunday, August 6, 2017

Mule ESB - How to write a simple munit unit-test for a flow

In this post, I'm going to use pretty simple flow to demonstrate the 'munit' test.

For an example, we will assume that we have sub-flow as below
    <sub-flow name="decision-Sub_Flow1">
        <set-variable variableName="country" value="Sri Lanka" doc:name="Variable"/>
    </sub-flow>

In this sub-flow, what we do is, we are creating a flow variable call 'country' and setting the value as 'Sri Lanka'.

Steps to write a munit test for the above scenario.

Right click the 'src/text/munit' folder and New -> MUnit Test. Then you can set a proper name to the text file and then need to select the flow which we are going to test.

It will open-up the test-suite as below
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
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">

    <munit:config name="munit" doc:name="MUnit configuration"/>
    
    <spring:beans>
        <spring:import resource="classpath:demo-munit-test.xml"/>
    </spring:beans>
    
    <munit:test name="SubFlow1-test-suite-decision-Sub_Flow1Test" description="Test">
        <flow-ref name="decision-Sub_Flow1" doc:name="Flow-ref to decision-Sub_Flow1"/>
    </munit:test>
</mule>

In the above test-suite file, we can clearly see that 3 components such as munit-config, spring-beans, and munit-test.
1. munit-config - we are saying that this is a munit config file
2. spring-beans - we are importing the mule app which we are going to write a munit test.
3. munit-test -  the area where we write the test code.

In this example, we are setting the country name and need to check the value whether it is correct of not. So, we can use the 'assert-on-equals' to fulfill this requirement as below
<munit:assert-on-equals expectedValue="#['Sri Lanka']" actualValue="#[flowVars.country]" doc:name="Assert Equals" message="Did not get expected value. expected value is 'Sri Lanka' but received #[flowVars.country]"/>

expectedValue: need to give the expected value in here, in this example it would be 'Sri Lanka'.
actualValue: here we need to give the actual value. we have set the flow variable call country, so, we can access that value as '#[flowVars.country]'
message: if this test fails, what would be the message to print.

After added this final munit test suite as below
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
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">

    <munit:config name="munit" doc:name="MUnit configuration"/>
    
    <spring:beans>
        <spring:import resource="classpath:demo-munit-test.xml"/>
    </spring:beans>
    
    <munit:test name="SubFlow1-test-suite-decision-Sub_Flow1Test" description="Test">
        <flow-ref name="decision-Sub_Flow1" doc:name="Flow-ref to decision-Sub_Flow1"/>
        <munit:assert-on-equals expectedValue="#['Sri Lanka']" actualValue="#[flowVars.country]" doc:name="Assert Equals" message="Did not get expected value. expected value is 'Sri Lanka' but received #[flowVars.country]"/>
    </munit:test>
</mule>

Saved and 'Run MUnit suite'

You can see the log file as below
===============================================================
===========  Running  SubFlow1-test-suite.xml  test ===========
===============================================================
Running SubFlow1-test-suite-decision-Sub_Flow1Test
SUCCESS - Test SubFlow1-test-suite-decision-Sub_Flow1Test finished Successfully.
INFO  2017-08-06 12:31:02,615 [main] org.mule.construct.FlowConstructLifecycleManager: Stopping flow: demo-munit-testFlow
INFO  2017-08-06 12:31:02,615 [main] org.mule.processor.SedaStageLifecycleManager: Stopping service: demo-munit-testFlow.stage1
INFO  2017-08-06 12:31:02,721 [main] org.mule.construct.FlowConstructLifecycleManager: Stopping flow: decision-Flow
INFO  2017-08-06 12:31:02,722 [main] org.mule.processor.SedaStageLifecycleManager: Stopping service: decision-Flow.stage1
INFO  2017-08-06 12:31:02,921 [main] org.mule.construct.FlowConstructLifecycleManager: Stopping flow: SubFlow1-test-suite-decision-Sub_Flow1Test
INFO  2017-08-06 12:31:02,921 [main] org.mule.processor.SedaStageLifecycleManager: Stopping service: SubFlow1-test-suite-decision-Sub_Flow1Test.stage1
INFO  2017-08-06 12:31:03,123 [main] org.mule.lifecycle.AbstractLifecycleManager: Stopping model: _muleSystemModel
INFO  2017-08-06 12:31:03,153 [main] org.mule.module.http.internal.listener.DefaultHttpListenerConfig: Stopped listener on http://0.0.0.0:8086
INFO  2017-08-06 12:31:03,155 [main] org.mule.util.queue.QueueXaResourceManager: Stopping ResourceManager
INFO  2017-08-06 12:31:03,155 [main] org.mule.util.queue.QueueXaResourceManager: Stopped ResourceManager
Connected to localhost in port 54275
INFO  2017-08-06 12:31:03,269 [Thread-1] org.mule.munit.plugins.coverage.server.MunitCoverageServer: Coverage connection received from localhost - true
INFO  2017-08-06 12:31:03,270 [main] org.mule.lifecycle.AbstractLifecycleManager: Disposing RegistryBroker
INFO  2017-08-06 12:31:03,274 [main] org.mule.construct.FlowConstructLifecycleManager: Disposing flow: demo-munit-testFlow
INFO  2017-08-06 12:31:03,277 [main] org.mule.processor.SedaStageLifecycleManager: Disposing service: demo-munit-testFlow.stage1
INFO  2017-08-06 12:31:03,278 [main] org.mule.construct.FlowConstructLifecycleManager: Disposing flow: decision-Flow
INFO  2017-08-06 12:31:03,278 [main] org.mule.processor.SedaStageLifecycleManager: Disposing service: decision-Flow.stage1
INFO  2017-08-06 12:31:03,278 [main] org.mule.construct.FlowConstructLifecycleManager: Disposing flow: SubFlow1-test-suite-decision-Sub_Flow1Test
INFO  2017-08-06 12:31:03,278 [main] org.mule.processor.SedaStageLifecycleManager: Disposing service: SubFlow1-test-suite-decision-Sub_Flow1Test.stage1
INFO  2017-08-06 12:31:03,280 [main] org.mule.lifecycle.AbstractLifecycleManager: Disposing model: _muleSystemModel
[org.mule.munit.remote.CoverageManager]accumulating report
[org.mule.munit.remote.CoverageManager]report is not null
INFO  2017-08-06 12:31:03,297 [Thread-1] org.mule.munit.plugins.coverage.server.MunitCoverageServer: Waiting for coverage connection 
INFO  2017-08-06 12:31:03,403 [main] org.mule.munit.runner.spring.config.MunitApplicationContext: Closing org.mule.munit.runner.spring.config.MunitApplicationContext@553f1d75: startup date [Sun Aug 06 12:30:57 IST 2017]; root of context hierarchy
INFO  2017-08-06 12:31:03,590 [main] org.mule.DefaultMuleContext: 
**********************************************************************
* Mule Context shut down normally on: 8/6/17 12:31 PM                *
* Server was up for: 0 days, 0 hours, 0 mins, 1.386 sec              *
**********************************************************************

===================================================================================
Number of tests run: 1 - Failed: 0 - Errors: 0 - Skipped: 0 - Time elapsed: 0.185ms
===================================================================================

Enjoy..!!

4 comments: