Unit test Mule mail endpoint using wiser smtp server

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="[email protected]" to="[email protected]" 
                    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
blog comments powered by Disqus