personal experiences and code :)

Showing posts with label dumbster. Show all posts
Showing posts with label dumbster. Show all posts

Tuesday, October 24, 2006

get Wiser with SubEthaSMTP

You won't think it should be so difficult to write effective test cases for the bits of your application where you have to send/receive emails.

Sample use case:

You have a 2-step registration process where after a user has submitted their info, they are sent a challenge token (via email) to validate the submission/make sure the email address is actually in use (and that they have access to it), before you add them to your 'users' table.

Ideally, to test this little piece of functionality, you need to be able to give your test mua a fake smtp port, have a dummy server listening on that port, have a convenient way of starting this server, retrieving messages it has to transfer, be able to retrieve all the usual suspects from a sent message (sender, receiver, subject, content etc), and be able to make assertions against these and your test data.

A long time ago, I found dumbster; I figured it solved the author's problems when he/she wrote it then, but just couldn't cut it for what I wanted to use it for.

So I went asearching, and found greenmail, which seemed to relieve me of the pains I was having with dumbster, although it still felt kinda kludgy; for instance, this is a snippet taken of the examples doc on the greenmail page:

[code]
servers = new Servers(ServerSetupTest.ALL); servers.start();
//use random content to avoid potential residual lingering problems final String
subject = servers.util().random(); final String body = servers.util().random();

//wait for max 5s for 1 email to arrive
assertTrue(servers.waitForIncomingEmail(5000, 1)); //Retrieve using GreenMail API
Message[] messages = servers.getReceivedMessages();
[/code]


the servers.waitForIncomingEmail(5000, 1) bit is clearly kludgy; it also seemed to treat ccs as sent mail, so if you sent one mail and cc'ed it to 10 people, then you had to remember to test for 11 mails to be sent by the mta; i found workarounds for this, and all in all it caused me far less pain than dumbster, so I was actually quite happy with it... at least until Geert pointed me to Wiser

From the Wiser page:

[code]
Using Wiser is trivial:

Wiser wiser = new Wiser();
wiser.setPort(2500); // Default is 25
wiser.start();

Now, use a mail client to send email to your Wiser server. To get your JavaMail messages out of the Wiser server, just use this code:

for (WiserMessage message : wiser.getMessages())
{
String envelopeSender = message.getEnvelopeSender();
String envelopeReceiver = message.getEnvelopeReceiver();

MimeMessage mess = message.getMimeMessage();

// now do something fun!
}
[/code]


It really doesn't get simpler (and wiser than that); and yes, writing effective email test cases has, thanks to Wiser, become quite simple; and fun too :)

cheers.

-- eokyere :)