Andrej Koelewijn

  • Home
  • About
  • Departments
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

Unit test Mule mail endpoint using wiser smtp server

October 21st, 2009  |  Published in oss, soa  |  1 Comment

Here’s a quick example (thx enno) how you can unit test an smtp endpoint in Mule using subethasmtp Wiser.

We’ll start by creating a new mule project using maven:

mvn mule-project-archetype:create -DgroupId=nl.iteye \
  -DartifactId=mule-mail -DmuleVersion=2.2.1 -Dinteractive=false \
  -Dtransports=smtp,vm -Dmodules=

If you have problems with dependencies when you run mvn compile, here’s a blog post that will explain how to fix the osgi dependency problems.

Next we need to add the subethasmtp dependency to the maven pom:

<dependency>
  <groupId>org.subethamail</groupId>
  <artifactId>subethasmtp</artifactId>
  <version>3.1.1</version>
</dependency>

We’ll create a simple Mule service which accepts a Mule message on a vm endpoint, and sends the message to an smtp server running on localhost. Here’s the mule-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.2"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.2"
       xmlns:smtp="http://www.mulesource.org/schema/mule/smtp/2.2"
       xsi:schemaLocation="

http://www.mulesource.org/schema/mule/core/2.2

http://www.mulesource.org/schema/mule/core/2.2/mule.xsd

http://www.mulesource.org/schema/mule/vm/2.2

http://www.mulesource.org/schema/mule/vm/2.2/mule-vm.xsd

http://www.mulesource.org/schema/mule/smtp/2.2

http://www.mulesource.org/schema/mule/smtp/2.2/mule-smtp.xsd">

    <model name="main">
       <service name="MailService">
            <inbound>
                <vm:inbound-endpoint path="mail-service" synchronous="true"/>
            </inbound>
            <outbound>
                <pass-through-router>
                    <smtp:outbound-endpoint host="localhost" port="44552"
                    from="test@example.com" to="test@example.com"
                    subject="Please read..." synchronous="true"/>
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

And finally the unit test code. The maven archetype has already created the skeleton code for this, which you can find in src/test/java/…, we just need to fill the test method. First we start an stmp server, then we call the mule service, and finally we can validate the results.

package org.mule.application.mulemail;

import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;
import org.mule.tck.FunctionalTestCase;
import org.mule.transport.NullPayload;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
import java.util.List;

public class MulemailTestCase extends FunctionalTestCase
{
    protected String getConfigResources()
    {
        return "mule-config.xml";
    }

    public void testMulemail() throws Exception
    {
    	Wiser smtpServer = new Wiser();
	smtpServer.setPort(44552);
	smtpServer.start();

        MuleClient client = new MuleClient();
        MuleMessage result = client.send("vm://mail-service", "some data", null);
        assertTrue("Result of service call is null", result != null);
        List<WiserMessage> messages = smtpServer.getMessages();
        System.out.println("Number of messages: " + messages.size());
        assertTrue("Incorrect number of messages found on mailserver",
          messages.size() == 1);
        assertTrue("Incorrect mail contents", messages.get(0).
          getMimeMessage().getContent().equals("some data"));
        smtpServer.stop();
    }
}

You can run the unit test using the following command.

mvn clean -Dtest=MulemailTestCase test
Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • SphereIt
  • StumbleUpon
  • Technorati
  • LinkedIn
  • HackerNews
  • PDF
  • Digg
  • Facebook
  • FriendFeed
  • Posterous
  • Tumblr
  • Twitter
  • RSS

View Comments

Feed Trackback Address
  1. Jan Vissers says:

    October 21st, 2009 at 8:15 pm (#)

    Nice one! I’ve used ‘Dumbster’ in the past for testing mail functionality (http://quintanasoft.com/dumbster). I sure will take a look at subethasmtp Wiser.

Leave a Response

blog comments powered by Disqus

Tags

bi bpel camel cep css dsl esb esper google governance grails groovy gtalk html5 innovation internet ipad ivy java javascript jaxrs jersey jigsaw jquery linkeddata linux maven middleware mule noiv openoffice openweb oracle osgi oss plsql rdbms rest soa sql sun tablet web 2.0 xmpp yql

Archives

  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Nice Java Decompiler tool: JD
  • VMware Player: The virtual machine is busy.
  • Adding a maven repository for installing features to ServiceMix
  • Upgrade Apache Camel in ServiceMix to version 2.3.0
  • A composite Rest service using Apache Camel

Categories

  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • Buddhika on Using google talk from java example
  • Anonymous on A composite Rest service using Apache Camel
  • Guest on How to find true cause of com.sun.star.uno.RuntimeException?
  • Absent Code attribute in method that is not native or abstract « Gooder Code - web development blog, php, java, asp.net, html, javascript on Absent Code attribute in method that is not native or abstract
  • Rmfume on Oracle best thing that could happen to JavaFX?
Buzz
andrkoel: RT @monkchips: James Governor's Monkchips » Day of The Dead: Web Drives Strong Demand for Java Skills http://monk.ly/d4UlND
2 hours ago, comment
andrkoel: RT @monkchips: In which my business partner @sogrady explains Why You Should Pay Attention to Node.Js http://monk.ly/a4aGIP serverside # ...
8 hours ago, comment
andrkoel: RT @stilkov: http://bit.ly/cDdqgl - AWS Identity and Access Management — I'd hate to have to compete against Amazon's Cloud offerings
14 hours ago, comment
andrkoel: Twitter for ipad is nice, but i still think i need a tool to summarize all info, something like feedly or flipboard is the future
8:36 AM Sep 02, 2010, comment
andrkoel: Trying out the new twitter for ipad... Curious how the panels work.
8:32 AM Sep 02, 2010, comment
 


©2010 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.