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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Trending

  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • A Hands-On ABAP RESTful Programming Model Guide
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  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.6K 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
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook