DZone
Performance Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Performance Zone > 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.

Ganesh Kumar Ravichandran user avatar by
Ganesh Kumar Ravichandran
·
Oct. 12, 17 · Performance Zone · Tutorial
Like (1)
Save
Tweet
7.14K 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.

Popular on DZone

  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • Java: Why Core-to-Core Latency Matters
  • Open Source Security Risks
  • Refactoring Java Application: Object-Oriented And Functional Approaches

Comments

Performance Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo