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

  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
  • Why Testing is a Long-Term Investment for Software Engineers

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Prioritizing Cloud Security Risks: A Developer's Guide to Tackling Security Debt
  1. DZone
  2. Coding
  3. Java
  4. How to Write JUnit for Mule Java Transformers and OnCall Classes

How to Write JUnit for Mule Java Transformers and OnCall Classes

In this quick tutorial, you'll learn how to use the popular testing framework, JUnit, to test the Java funcationalities within your Mule app.

By 
Ganesh Kumar Ravichandran user avatar
Ganesh Kumar Ravichandran
·
Oct. 12, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
8.3K 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?

MUnits are used to test Mule Applications. But, in order to do functional test cases specific to a Java file, we can't use MUnit. Hence we need to write JUnit in Mule to test the Java functionalities. In order to do that we need to:

1. Create a test class under src\test\java.

2. Set the name as TestJUnit.java.

3. Set the package name as com.junit.test.

4. Set the superclass as AbstractMuleContextTestCase (you can set the SuperClass as FunctionalTestCase in case you also want to test the flow in your JUnit).

5. Click the Finish button.

Here, setting the superclass as AbstractMuleContextTestCase reduces our need to invoke the getResources() method, which avoids having to add the Mule Configuration File for testing.

Set Java File

6. Write your own test case, just like we do in regular JUnit, through the Test Annotation.

package com.junit.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
import org.mule.tck.junit4.AbstractMuleContextTestCase;
import org.springframework.core.io.ClassPathResource;

import com.app.JavaTransformer;

public class TestJUnit extends AbstractMuleContextTestCase {
@Test
public void testJavaTransformation() throws Exception {
JavaTransformer ht = new JavaTransformer();
String outputEncoding = "ISO 10646/Unicode(UTF-8)";
String payload = readFile("sample_data.json");
MuleEvent event = getTestEvent(payload, muleContext);
event.setFlowVariable("dummy1", "value");
MuleMessage message = event.getMessage();
Assert.assertNotNull(new JavaTransformer().transformMessage(message, outputEncoding));
}

private String readFile(String resourcePath) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(new ClassPathResource(resourcePath).getFile()))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
sb.append(sCurrentLine);
}
} catch (IOException e) {
Assert.fail();
}
return sb.toString();
}
}

7. This example reads the payload for the Java class from a JSON file present in src\test\resources.

8. Similarly, for the on-call method, the code looks like what I've done below.

package com.junit.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.mule.DefaultMuleEventContext;
import org.mule.api.MuleEvent;
import org.mule.tck.junit4.AbstractMuleContextTestCase;
import org.springframework.core.io.ClassPathResource;

import com.app.JavaTransformer2;

public class TestJUnit extends AbstractMuleContextTestCase {
@Test
public void testHierTest() throws Exception {
String payload = readFile("sample_data.json");
MuleEvent event = getTestEvent(payload, muleContext);
event.setFlowVariable("dummy1", "value");
Assert.assertNotNull(new JavaTransformer2().onCall(new DefaultMuleEventContext(event)));
}

private String readFile(String resourcePath) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(new ClassPathResource(resourcePath).getFile()))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
sb.append(sCurrentLine);
}
} catch (IOException e) {
Assert.fail();
}
return sb.toString();
}
}

9. If you want to set any flow variable, set it with the event.SetFlowVariabale() method.

10. If you want to initialize or close before the test begins and ends, use the JUnit Annotations appropriately.

Java (programming language) JUnit Testing

Opinions expressed by DZone contributors are their own.

Related

  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
  • Why Testing is a Long-Term Investment for Software Engineers

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!