Automated Testing of E-Mail Functionality

Nowadays most (web) applications have to send e-mails for notifications, news, passwords, reminders and so on. You have to ensure that the correct e-mail is really sent to the receiver and it is a good idea to do this using an automated test. Since you usually do not want to depend on the whole network infrastructure and externally installed mail servers which could cause breaking tests without code change once in a while some kind of mock mail server would be nice.

We tried two solutions in our grails web application:

Dumbster

A really simple fake smtp server written in Java. Using it is really simple: You just add the jar to your project and start the smtp server on some port:

SmtpServer mailServer = SmtpServerFactory.startServer(2525);

Your application can now simply send e-mails using the smtp-server running locally on the specified port. In your tests you can check the mail server for the expected e-mails. After each test we stopped dumpster resetting it to a clean state.

mailServer.stop()

The problem we had with this solution was, that stopping the fake smtp was not enough between tests. There were zombie dumpster threads left which caused an excessive load. Our acceptance test run slowed down increasingly with every new test. Eventually, execution time of acceptance tests doubled to more than two hours!

Greenmail (plugin for grails)

Due to the problems with dumbster we tried greenmail with its grails plugin. The greenmail smtp is available as a spring bean in grails and is mostly transparent as a plugin. We only had to change our WithEmailServer rule which provides a clean and enabled state and disables greenmail after the test using the rule.

    void before() {
        SpringUtil.getBean('grailsApplication').config.grails.mail.disabled = false
        SpringUtil.getBean('greenMail').deleteAllMessages()
    }

    void after() {
        SpringUtil.getBean('grailsApplication').config.grails.mail.disabled = true
    }

If your tests are executing in a different JVM than your app is running, you may need to use the Remote Control-plugin to access greenMail or grailsApplication. The grails greenmail plugin provides a controller too, which lets you inspect sent messages over the web interface.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 82 other followers

%d bloggers like this: