Notification Service: Testing e-mail with Dumbster
19 November 2008After I built the EmailCourier, I wanted to write a little test for it, but I wasn’t quite sure how to do that without launching an e-mail to someone every time I ran a build. Then I came across Dumbster:
The Dumbster is a very simple fake SMTP server designed for unit and system testing applications that send email messages. It responds to all standard SMTP commands but does not deliver messages to the user. The messages are stored within the Dumbster for later extraction and verification.
The Dumbster slots itself very easily into your testing strategy. As long as your application talks to an email server using SMTP then the Dumbster can be used to test the application with no code changes.
Pretty cool, huh?
So, I added this little tidbit to the project’s pom.xml:
<dependency>
<groupId>dumbster</groupId>
<artifactId>dumbster</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
… and then wrote a little test:
package org.restafarian.notify.service.impl;
import java.util.Iterator;
import org.restafarian.notify.beans.MessageAddress;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
public class EmailCourierTest extends AbstractDependencyInjectionSpringContextTests {
private EmailCourier emailCourier;
protected String[] getConfigLocations() {
setAutowireMode(AUTOWIRE_BY_NAME);
return new String[] {"/context/applicationContext*.xml"};
}
/**
* Tests the EmailCourier
*/
public void testDeliverMessage() {
// start the dummy smtp server
SimpleSmtpServer server = SimpleSmtpServer.start();
// set up the addresses for this test
MessageAddress[] addresses = new MessageAddress[2];
MessageAddress fromAddress = new MessageAddress();
fromAddress.setAddressType("mailFrom");
fromAddress.addAddressValue("fromAddress@localhost");
addresses[0] = fromAddress;
MessageAddress toAddress = new MessageAddress();
toAddress.setAddressType("mailTo");
toAddress.addAddressValue("toAddress@localhost");
addresses[1] = toAddress;
// run the test
emailCourier.deliverMessage(addresses, "Test Message", "text/plain", "This is a test.");
// stop the dummy smtp server
server.stop();
// verify the results
assertTrue(server.getReceivedEmailSize() == 1);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage)emailIter.next();
assertTrue(email.getHeaderValue("Subject").equals("Test Message"));
assertTrue(email.getBody().equals("This is a test."));
}
/**
* @return the emailCourier
*/
public EmailCourier getEmailCourier() {
return emailCourier;
}
/**
* @param emailCourier the emailCourier to set
*/
public void setEmailCourier(EmailCourier emailCourier) {
this.emailCourier = emailCourier;
}
}
… and it worked!
OK, it wasn’t really that simple … I had to set up the Spring configuration in the applicationContext.xml file, which I had forgotten to do the first time, and then I had to fix some errors in the EmailCourier, which were discovered during testing (hey — what an idea!), but after all of that, it worked, which was still pretty cool. With this little tool added to the portfolio, there are a lot of other tests that I can create now, but we’ll save that for another day.
Leave a reply
You must be logged in to post a comment.






