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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Mule 4: Dynamically Call Multiple Subflows With One Flow-Ref
  • Handle HL7 MLLP Messages With Mule 4
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ

Trending

  • AI’s Role in Everyday Development
  • Agentic AI for Automated Application Security and Vulnerability Management
  • How Trustworthy Is Big Data?
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  1. DZone
  2. Data Engineering
  3. Databases
  4. MUnit Testing With Mulesoft: Part II (Mock Message Processor)

MUnit Testing With Mulesoft: Part II (Mock Message Processor)

The Mock component is useful in various scenarios of Mule application testing. In this part of the series, we will see how to use the Mock component in an MUnit test.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
May. 01, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
25.4K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial continues from Part 1.

Mock is a feature provided by MUnit to mock the behavior of the message processor. Basically, MUnit replaces the actual behavior of message processor with the behavior defined by us.

There are various scenarios where we can use the Mock Message processor. We have completed the development of your Mule application and need to test it, but in our application, there is a database connector in the message processor region and the database is not ready to accept any request; but, we still want to test the application. In such cases, we can mock the normal database connector behavior.

There can be more scenarios, like we are connecting to JMS queue but JMS is not ready, and we want to test the application. In such cases, we can again make use of mock message processor.

Let's walk through how to use the Mock component in MUnit test. Below is the flow for which we will create MUnit tests and will mock set-payload component.

Image titleRight click the flow and create the MUnit tests. It will create MUnit tests in the folder src/test/munit of your Mule application.Image title

Define the Message Processor to Mock

Drag and drop the Mock component in the setup region of MUnit test.

Image title

  <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
            <mock:then-return payload="#['Sample Message']"/>
        </mock:when>

 messageProcessor  is used to specify the message processor to be mocked. In this case, we have mock set-payload component.

Define the Mock with Attributes

When we have more than one similar component (i.e. 2 set-payload) in our Mule flow but we want to mock only one set payload. In that case, we need to add attributes in the Mock component which accept name and value of message processor.

Below is the Mule flow having 2 set-payload and we need to mock only one component.

Image title

We can define attributes as shown below to mock one set-payload.

Image title

   <mock:when messageProcessor="mule:set-payload" doc:name="Mock">
            <mock:with-attributes>
                <mock:with-attribute name="doc:name" whereValue="setPayload1"/>
            </mock:with-attributes>
            <mock:then-return payload="#['Sample Message']"/>
        </mock:when>
  •  name  is the name of attribute and it cannot be MEL expressions.

  •  whereValue  is the value that the attribute of the real message processor should contain. It accepts MEL expressions.

Below is the video tutorial:


Now, you know how to mock the message processor with MUnit.

Testing application MuleSoft Attribute (computing) Flow (web browser) Database Connector (mathematics) Drops (app) Payload (computing) Requests

Opinions expressed by DZone contributors are their own.

Related

  • Mule 4: Dynamically Call Multiple Subflows With One Flow-Ref
  • Handle HL7 MLLP Messages With Mule 4
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ

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!