DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Any Version of the Test Pyramid Is a Misconception – Don’t Use Anyone
  • Error Handling Inside Kumologica Subflow
  • Two Cool Java Frameworks You Probably Don’t Need
  • Fighting Fragility With Property-Based Testing

Trending

  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • Rust, WASM, and Edge: Next-Level Performance
  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Monolith: The Good, The Bad and The Ugly
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Mule MUnit Testing With Variables and Properties

Mule MUnit Testing With Variables and Properties

Learn how to set up and use MUnit to test variables in your Mule flows and subflows.

By 
Manik Magar user avatar
Manik Magar
·
Jul. 26, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
21.3K Views

Join the DZone community and get the full member experience.

Join For Free

Who doesn't love a good test? When it comes to integrating moving parts, testing is one of the most important phases of any project. Flow Variables, Session Variables, and Inbound/Outbound properties are very common in Mule flows. Fortunately, Mule MUnit framework makes it very easy to unit test any Mule Flow and subflows. In this post, we will see how we can unit test our flows involving variables and properties.

Let's consider a simple Mule flow that reads a file and then sets a flow variable and session variable:

Mule Munit Flow


<flow name="SampleMuleFlow">
    <file:inbound-endpoint path="input" responseTimeout="10000" doc:name="File"/>
    <set-variable variableName="fileName" value="#[message.inboundProperties.originalFilename]" doc:name="Variable"/>
    <set-session-variable variableName="sessFileName" value="#[flowVars.fileName]" doc:name="Session Variable"/&gt
    <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>


Creating an XML MUnit Test Case

If you are using Anypoint Studio, then you can right click our flow, choose MUnit –> Create New Suite. Studio will automatically create an MUnit Test suite under /src/test/munit and add a test case to it. It should look like:

testmule-munit-xml-testcase


What We Will Do Here

  1. MUnit mocks all inbound connectors and endpoints, so we will need to manually set the test message for the flow. We will do that by adding a <munit:set> component. We will also set an inbound property originalFilename and invocation/flow variable on the message.
  2. Call our main flow with using flow-ref.
  3. After the main flow is executed, we will verify that flow variable value is same as what we set on message.

Our final XML Unit Test:


<munit:test name="testmule-test-suite-SampleMuleFlowTest" description="Test">
    <munit:set payload="#[getResources('test.txt').asStream()]" doc:name="Set Message">
        <munit:invocation-properties>
            <munit:invocation-property key="fileName2" value="test2.txt"/>
        </munit:invocation-properties>
        <munit:inbound-properties>
            <munit:inbound-property key="originalFilename" value="test.txt"/>
        </munit:inbound-properties>
    </munit:set>
        <flow-ref name="SampleMuleFlow" doc:name="Flow-ref to SampleMuleFlow"/>
        <munit:assert-on-equals expectedValue="#[flowVars.fileName2]" actualValue="#['test2.txt']" doc:name="Assert Equals"/>
    </munit:test>


We can then run this as a MUnit Test in studio.

Creating Java MUnit Test Case

For those who prefer writing Java instead of XML, MUnit framework provides fluent Java APIs for JUnit. You can create java class by extending FunctionalMunitSuite class.

Below is our java test case:

package testmule;

import java.io.InputStream;

import org.junit.Assert;
import org.junit.Test;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
import org.mule.api.transport.PropertyScope;
import org.mule.munit.runner.functional.FunctionalMunitSuite;

public class SampleTestCase extends FunctionalMunitSuite {

    @Override
    protected String getConfigResources() {
        return "testmule2.xml";
    }

    @Test
    public void testSampleFlow(){
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.txt");
        MuleMessage msg = muleMessageWithPayload(is);
        try {
            msg.setProperty("originalFileName", "test.txt", PropertyScope.INBOUND);
            MuleEvent test = testEvent(msg);

            test.setFlowVariable("fileName2", "test2.txt");

            MuleEvent reply = runFlow("SampleMuleFlow", test);

            Assert.assertEquals("Verify Flow Variable", "test2.txt", reply.getFlowVariable("fileName2"));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}


Points to note:

  1. Create a MuleMessage using muleMessageWithPayload function provided by the FunctionalMunitSuite super class.
  2. Create a MuleEvent using testEvent function provided by FunctionalMunitSuite super class.
  3. Inbound and Outbound properties can be set on the Message we created. For flow variables and session variables, we will use the test MuleEvent. The testEvent(msg) method does not copy flow variables and session variables into test events, so setting anything will get lost while creating that test event.
  4. Finally, we call our main flow and verify the value of our flow variable in the returned message. So, keep testing your code!

More Reading:

MUnit Documentation

unit test Property (programming) Flow (web browser) Test case

Published at DZone with permission of Manik Magar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Any Version of the Test Pyramid Is a Misconception – Don’t Use Anyone
  • Error Handling Inside Kumologica Subflow
  • Two Cool Java Frameworks You Probably Don’t Need
  • Fighting Fragility With Property-Based Testing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!