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.
Join the DZone community and get the full member experience.
Join For FreeOnce 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.
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.
Opinions expressed by DZone contributors are their own.
Comments