Dashboard > Mule FAQ - Open Source ESB and Integration Platform > ... > Frequently Asked Questions (FAQ) > How do I write a simple jUnit test case
  Mule FAQ - Open Source ESB and Integration Platform Log In | Sign Up   View a printable version of the current page.  
  How do I write a simple jUnit test case

Added by jason ball , last edited by Jonathan Appel [CustomWare] on Feb 28, 2007  (view change)
Labels: 
(None)

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://test","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:

================================================================================
= Testing: Mule Server Test(MuleServerTest)&nbsp;&nbsp; =
================================================================================
log4j:WARN No appenders could be found for logger (org.mule.config.MuleDtdResolver).
log4j:WARN Please initialize the log4j system properly.

Got Response: OK - Completed Event: hello





Copyright(c) CustomWare Asia Pacific Pty Ltd
Powered by Atlassian Confluence 2.7.3, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators