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:


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"
    <model name="main">
       <service name="MailService">
                <vm:inbound-endpoint path="mail-service" synchronous="true"/>
                    <smtp:outbound-endpoint host="localhost" port="44552" 
                    from="[email protected]" to="[email protected]" 
                    subject="Please read..." synchronous="true"/>

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();
        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"));

You can run the unit test using the following command.

mvn clean -Dtest=MulemailTestCase test
blog comments powered by Disqus