Unittesting e-mail sending using Spring

In previous projects I mostly skipped writing tests for sending mail. Mostly due to the fact that there is a dependency on a working SMTP server. This time I decided to invest some time in finding a solution for this problem.

It didn’t have to look very far to find Wiser. Wiser is a very simple fake SMTP server designed for unit and system testing applications. You basically start it up, send mail to it and read the queue of messages it received.

Using it in unit test is indeed simple. First setup the server, and reconfigure mail senders in the Spring context:

[java]
@SuppressWarnings(“unchecked”)
@Override
protected void onSetUp() throws Exception {
super.onSetUp();
wiser = new Wiser();
wiser.setPort(2525);
wiser.start();

Map ofType =
getApplicationContext().getBeansOfType(org.springframework.mail.javamail.JavaMailSenderImpl.class);

// reconfigure mailsenders in the spring context
for (Entry bean : ofType.entrySet()) {
log.info(String.format(“configuring mailsender %s to use local Wiser SMTP”, bean.getKey()));
JavaMailSenderImpl mailSender = bean.getValue();
mailSender.setPort(2525);
mailSender.setHost(“localhost”);
}
}
[/java]

Now you are ready to run a test:

[java]
public void testSendMailStrings() {
mailService.sendMail(“test-sender@test.nl”, “test-reciever@test.nl”, “testing the mailservice”, “testing the mail service, body”);

assertEquals(1, wiser.getMessages().size());
WiserMessage m = wiser.getMessages().get(0);
assertEquals(“test-sender@test.nl”, m.getEnvelopeSender());
assertEquals(“test-reciever@test.nl”, m.getEnvelopeReceiver());
try {
MimeMessage message = m.getMimeMessage();
assertEquals(“testing the mailservice”, message.getSubject());
assertEquals(“testing the mail service, body”, (String)message.getContent());
} catch (MessagingException me) {
fail(String.format(“MessagingException [%s] should not occur”, me.getMessage()));
} catch (IOException ioe) {
fail(String.format(“IOException [%s] should not occur”, ioe.getMessage()));
}
}
[/java]

And after this, make sure to stop the server so it can be started again for consequent tests:

[java]
@Override
protected void onTearDown() throws Exception {
wiser.stop();
}
[/java]

Works like a dream!

This entry was posted in e-mail, java, junit, smtp, spring, wiser. Bookmark the permalink.

6 Responses to Unittesting e-mail sending using Spring

  1. erikvanoosten says:

    Indeed, SubethaSMTP is cool. I used it in my embedded e-mail server for Mule.

  2. peter says:

    I knew I had seen it somewhere before!

  3. Stephane says:

    How to delete previously sent messages from the wiser.getMessages() list..?

    Stopping Wiser 1.2 does not delete the messages.

    The problem is that it makes wiser difficult to use since tests become inter dependant.

  4. Stephane says:

    My tests are inter dependant and fail due to the fact that I cannot start each test with an empty message list.

    Stopping and starting Wiser does not empty the list of previously sent messages as I expected it to do.

    The following code does display the messages that were previously sent.

    @Override
    protected void tearDown() throws Exception {
    System.err.println(“Stopping Wiser”);
    wiser.stop();
    for (WiserMessage wm : wiser.getMessages()) {
    System.err.println(“Receiver ” wm.getEnvelopeReceiver());
    System.err.println(“Sender ” wm.getEnvelopeSender());
    try {
    System.err.println(“Content ” wm.getMimeMessage().getContent());
    } catch (IOException e) {
    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (MessagingException e) {
    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    }
    System.err.println(“Stopped Wiser”);
    }

  5. Stephane says:

    By the way, I’m using the following version of wiser:

    org.subethamail
    subethasmtp-smtp
    1.2

    org.subethamail
    subethasmtp-wiser
    1.2

  6. damo says:

    @Stephane

    The most straightforward way of clearing the messages is to call wiser.getMessages().clear().

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>