Testing is key activity of every software development projects. However testing certain features is not easy and need special support functions. One of such functionality is email testing. In this article I introduce few tools that I used in my projects for achieving email testing easily without any side effects.

To test emails effectively we end up using real email addresses, which leads to cluttering mailbox with unwanted emails. For testing different scenarios we need multiple email address, and testing becomes more difficult when it requires checking mailbox of other users. Also we never want to send emails to real users, otherwise they gets confused with test emails and real emails. Also we do not want to make any code modification for testing emails, this leads to code maintenance issues.

To overcome above problems, what we need is, Fake SMTP server (Email Service) which acts as outgoing server, however, it never delivers email message to users and provides a user interface to check and verify all outgoing emails. In short application sends email to users however it never goes out of SMTP server.

We need Fake SMTP server for following different scenarios,

1. Unit Testing

Embedded version with Assertion support for Unit Testing, so emails can be verified using asserts in unit tests. Dumbster is handy library here. Some frameworks like Rails has inbuilt support for unit testing emails.

 1public class TestEmail {
 2
 3    @Test
 4    public void testSend() {
 5        SimpleSmtpServer server = SimpleSmtpServer.start();
 6
 7        sendMessage("Subject", "Test Body", "abc@example.com");
 8
 9        server.stop();
10
11        assertThat(server.getReceivedEmailSize(),equalTo(1));
12
13        Iterator emails = server.getReceivedEmail();
14        SmtpMessage email = (SmtpMessage)emails.next();
15
16        assertThat(email.getHeaderValue("Subject"),equalTo("Subject"));
17        assertThat(email.getHeaderValue("To"),equalTo("abc@example.com"));
18        assertThat(email.getBody(),equalTo("Test Body"));
19    }
20}

2. Local Developer Box Testing

Local SMTP service with standalone application for developer box testing. So after development developers/testers can verify emails functionality locally.

  1. FakeSMTP No installation required. Just run a simple JAR file. Works on any platform.
  2. Papercut Windows only solution. Better UI than FakeSMTP but requires installation

3. Hosted Service

Hosted service with Web UI for Non-Production (QA, UAT, Staging) environments, which helps multiple tester to verify email functionality.

  1. Mailcatcher It has a web view which allows viewing messages from anywhere, ruby application that can runs independently.
  2. Mailtrap.io Cloud based SaaS solution, very sophisticated but paid.

mailcatcher.me

Conclusion

Using tools and techniques which are applicable for different stages of testing, we can effectively test all email scenarios without delivering emails to real users. Above solutions doesn’t require any change in production code for testing, just needs different configuration of SMTP server.

Happy Testing !!!