views:

40

answers:

3

I have a set of Java, email related unit tests that are currently using Wiser as a test SMTP server. It works great for basic email testing (send email, check to ensure it was received, etc.), but I have logic that I would like to exercise around retrying failed email sends based on error response returned from the SMTP server. Does anyone know of any test SMTP servers out there that support rigging them to respond in a prescribed way? Preferably, a pure Java server that could be spun up by the unit tests themselves would be best, but any stand alone server that fits the bill would work. Thanks.

A: 

You shouldn't really be calling anything external in your unit tests. Unit tests are supposed to be self contained. You should mock out all external services and states.

If you post some code on how you are calling the SMTP server someone should be able to help you mock it out. Are you using javax.mail?

Wes
A: 

Roll your own. It's fairly trivial to make a Java class that binds to a TCP port (like, say, port 25) and listens for traffic.

Once upon a time I did this on port 80 for some C# unit tests to simulate a web service.

Create different functions that respond in the various ways you need to handle, and you're done. It doesn't need to be a real SMTP server because this is a unit test. If you encounter a condition you didn't test for, add more tests.

mwalker
I am not currently using a real SMTP server. Wiser (http://code.google.com/p/subethasmtp/wiki/Wiser) and similar libraries (Dumbster) were written with unit tests in mind. They offer useful features such as received message counting that makes testing mail related code fairly straightforward. Of course, the messages never actually go anywhere. I could no doubt grab the source for Wiser and add the features I am looking for, but figured that they are common enough requests that perhaps something out there may already exist.
mreynolds0404
A: 

Kohsuke Kawaguchi wrote a JavaMail provider for unit tests, and I know that it supports some error condition simulation. You can take a look at it here: Mock JavaMail.

The error simulation is pretty simple. He says,

Mailbox can be marked as 'error' programatically, which causes all sending/receiving operations to fail. This can be used to test the error handling behavior of the application.

erickson