Mule provides a number of useful classes for writing junit tests for mule services. Using these classes will save a lot of development overhead when coding simple junit test cases. For example the following test case needs to:
1. Start a Mule Server.
2. Run some tests against one of the configured components.
3. Check the results of the tests to ensure the tests completed successfully.
The org.mule.tck.FunctionalTestCase provides the necessary setup() methods to initialise the mule server based on an existing config file. All that is necessary is to implement the getConfigResources method to define which config file to use.
On startup of the test the config file will be loaded and the server started.
jUnit test example:
import org.mule.umo.*;
import org.mule.tck.FunctionalTestCase;
import org.mule.extras.client.MuleClient;
import org.mule.providers.NullPayload;
public class MuleServerTest extends FunctionalTestCase {
public MuleServerTest() {
super();
}
protected String getConfigResources()
{
return"mule-config.xml";
}
public String getName() {
return "Mule Server Test";
}
public static void testTestCase() throws Exception {
MuleClient client = new MuleClient();
UMOMessage message = client.send("vm:,"hello", null);
assertNotNull(message);
assertFalse(message.getPayload() instanceof NullPayload);
System.out.println("Got Response: " + message.getPayloadAsString());
}
}
In this example the testTestCase sends a String to the queue vm://test.
The following test class is subscribed to that queue via the mule-config.xml.
public class testClass {
public String doEvent(String event) {
return("OK - Completed Event: " + event);
}
}
With the following config file configures the test class to receive messages on the vm://test queue:
<!DOCTYPE mule-configuration PUBLIC "-//MuleSource //DTD mule-configuration XML V1.0//EN"
"http://mule.mulesource.org/dtds/mule-configuration.dtd"><mule-configuration id="testClassConfig" version="1.0">
<connector name="VMConnector" className="org.mule.providers.vm.VMConnector"></connector>
<model name="testClass">
<mule-descriptor name="testClassUMO"
implementation="testClass"
inboundEndpoint="vm://test">
</mule-descriptor>
</model>
</mule-configuration>
When this test is run the following results should be seen: