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
Newsletter
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
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory

Trending

  • Mistaking Code Production for Engineering Progress: AI Productivity Myths Part 1
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Detection and Response Did Its Job. Now Someone Has to Actually Fix It.
  • Introducing senro: A Pipeline Engine You Program in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 12, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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