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

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

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

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

  • How to Test QR Codes in Your Applications
  • JUnit 5 Custom TestListeners
  • Getting Started With Microsoft Tool Playwright for Automated Testing
  • Injecting Chaos: Easy Techniques for Simulating Network Issues in Redis Clusters

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. MuleSoft: Functional Test Case

MuleSoft: Functional Test Case

MUnit is a Mule application testing framework which allows us to easily build automated tests. With MUnit, we can create Mule tests by writing common Java code.

By 
Sulthony H user avatar
Sulthony H
·
May. 30, 16 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
7.5K Views

Join the DZone community and get the full member experience.

Join For Free

Once we have utilized a Java Component in Anypoint Studio, the next challenge is: How do we test it?

For testing purposes, we can use MUnit. MUnit is a Mule application testing framework which allows us to easily build automated tests. With MUnit, we can create Mule tests by writing common Java code, slightly similar to Junit. Naturally, it is more comfortable for Java developers.

To practice this, we can use any Java class in any Anypoint project. However, it is suggested to follow the example in the previous post: MuleSoft: Method Entry Point Resolver, as the Java code example in this post refers to it.

  1. Open Package Explorer, then right click on src/test/java. Choose: New >> Class.

  2. Set the Package as: com.mulesoft.training

  3. Set the Name as: MathOperationTest

  4. Set the Superclass as: org.mule.tck.junit4.FunctionalTestCase

    • Note: we can type it directly or click the Browse button

  5. Click the Finish button

A new empty Java class will be created and opened:

package com.mulesoft.training;

import org.mule.tck.junit4.FunctionalTestCase;

public class MathOperationTest extends FunctionalTestCase {

}

Our Test class must extend FunctionalTestCase, a base test case for Mule functional tests. It contains a number of supporting classes and contexts, which will make it easy to do the testing.

Before completing our Test class, the first thing that we have to do is specify the configuration file to be tested. To do that, we have to override the getConfigResources() method and then specify the file name.

package com.mulesoft.training;

import org.mule.tck.junit4.FunctionalTestCase;

public class MathOperationTest extends FunctionalTestCase {
    protected String getConfigResources() {
        return "mathoperation.xml";
    }
}

Note: we can specify more than one file using a comma-separated list.

Now we will complete our simple mathematic operation by testing the MathOperation class directly through the mathoperationFlow.

To test the Java class directly, it is not different from a common JUnit operation. Please refer to the example below, and run a JUnit test by choosing: Run As >> JUnit Test

package com.mulesoft.training;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.mule.tck.junit4.FunctionalTestCase;

public class MathOperationTest extends FunctionalTestCase {
    protected String getConfigResources() {
        return "mathoperation.xml";
    }

    @Test
    public void testJavaClass() {
        String value = "4";
        MathOperation mathOperation = new MathOperation();
      
        assertEquals("16.0", mathOperation.power(value));
        assertEquals("2.0", mathOperation.squareRoot(value));
    }
}

By choosing this approach we can test the specific function. If required, we can test the flow to make sure its behavior is appropriate. For this purpose, add this snippet into the current Test class:

@Test
public void testFlow() throws Exception {
    MuleEvent event = runFlow("mathoperationFlow", "4");
    MuleMessage message = event.getMessage();

    assertNotNull(message);
    assertNotNull(message.getPayload());
    assertEquals("16.0", message.getPayload());
}

Here, we utilize runFlow to call the flow by its name, and send the payload as a second argument. The behavior of the rest of the code is similar when we run Mule on debug mode. We can evaluate the message, payload, etc.

Another approach to test our integration project is testing the inbound endpoint. It simulates the real execution of the flow by using MuleClient to interact with the running Mule server. Basically, there are many options for this interaction. In this example, we specify the URL as the first argument, the payload as the second argument, and set null for message properties (it is not needed in this example) as the third argument.

@Test
public void testInboundEndpoint() throws Exception {
    MuleClient client = new MuleClient(muleContext);
    MuleMessage message = client.send("http://localhost:8081/power", "4", null);

    assertNotNull(message);
    assertNotNull(message.getPayload());
    assertEquals("16.0", message.getPayloadAsString());
}

That is how to test a Java Component in Anypoint studio. So feel free to try the different options and choose which one suits your integration project.

Happy testing!

Testing Test case MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • How to Test QR Codes in Your Applications
  • JUnit 5 Custom TestListeners
  • Getting Started With Microsoft Tool Playwright for Automated Testing
  • Injecting Chaos: Easy Techniques for Simulating Network Issues in Redis Clusters

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!