Mule ESB Email testing with GreenMail

1. A core file that contains the services
<service name="SampleService">
	<inbound>
		<jms:inbound-endpoint queue="in"
			transformer-refs="JMSMessageToObject" />
	</inbound>
	<component class="org.mule.example.hello.Greeter" />
	// Above class will collect the payload and append with Hello(Hello
	+payload)
	<outbound>
		<pass-through-router>
			<smtp:outbound-endpoint port="${smtp.port}"
				host="${smtp.server}" from="${smtp.from.address}" subject="Accounting Invoice"
				to="${smtp.to.address}">
				<email:string-to-email-transformer />
			</smtp:outbound-endpoint>
		</pass-through-router>
	</outbound>
</service>

2. A file that contains Mule XML configuration appropriate for unit tests
Note that the jms definition fires up Apache Active-MQ inside a virtual machine (vm://localhost), but that the smtp definition is very generic.

<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" 
	...   
	<context:property-placeholder location="classpath*:test-config.properties" />
	<jms:activemq-connector name="jmsConnector"
		specification="1.1" brokerURL="vm://localhost" />
	<smtp:connector name="smtpConnector" />
</mule>


3. A file that contains Mule XML configuration appropriate for development
Note that we are using a Weblogic JMS queue definition here, rather than the Apache MQ definition used in our test file.
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2" ...   
	<context:property-placeholder location="classpath*:prod-config.properties" />
	<jms:activemq-connector name="jmsConnector"
		specification="1.1" brokerURL="jms://localhost:8030" />
	<smtp:connector name="smtpConnector" />
</mule>

4. A file that contains configuration properties appropriate for unit tests
   smtp.port=65438
   smtp.server=localhost
   smtp.from.address=test@example.com
   smtp.to.address=testdest@example.com 

5. A file that contains configuration properties appropriate for development
   smtp.port=25
   smtp.server=mail.example.com
   smtp.from.address=production@example.com
   smtp.to.address=dest@example.com

6. The unit test:
Now we take a quick look at the unit test, which just fires up an in-memory SMTP server running on port 3025, pushes a message into the in-memory JMS queue, and checks that the expected email is sent using the "Greenmail" apis:
   public class GreeterTest extends FunctionalTestCase {
	
 private GreenMail greenMail; 
 	protected void setUp() throws Exception {      
		 ServerSetup setup = new ServerSetup(65438, "localhost", "smtp");   
		 //Test properties      
		 GreenMail server = new GreenMail(setup);      
		 greenMail.start();
	} 
 	protected void tearDown() throws Exception {    
		 greenMail.stop(); 
	} 
	 public void testJmsToEmail() {    
		 MuleClient client = new MuleClient();    
		 String payload = "Bob";   
		 client.send("jms://in", new DefaultMuleMessage(payload));    
		 assertTrue(greenMail.waitForIncomingEmail(5000, 1));    
		 assertEquals( "Hello Bob", 
				 GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]);   
	}
 }

As with the previous cases, for a full example I suggest that you take a look at the Mule functional test cases for the FTP transport, starting with org.mule.transport.ftp.AbstractFtpServerTestCase.java.