views:

58

answers:

3

I want to unit test a Java application that fetches mails from an email inbox, much like this guy. Currently, I run the unit tests against a real mailbox on our company's real mailserver which was easy to set up, but has the following disadvantages:

  • You have to send actual emails before you run the test
  • Adding more test cases might be difficult, for example because you might want to test against different security policies
  • The test depends on a working network connection to the mail server and an existing mail account which couples development and system administration in a way that makes no sense to me.

I would like to fire up an IMAP server on a local port, which fakes an inbox based on test data stored in files alongside the test classes. I can think of the following approaches:

  • Run a socket server and implement a rudimentary IMAP subset
  • Use a higher level library made for building email servers
  • Use an existing email server implementation that I can embed in my tests

I would like to avoid the first option, it sort of looks straightforward, but I'm guessing from similar experience that there's a long tail of work waiting further down the road. Just think of wanting to test secure connections etc. Similarly, the second option seems like to much work, but I haven't found a mail server yet that would allow for the third one.

If it matters, I'm using Maven and TestNG during the build process.

+2  A: 

Write a test which relies on an existing mail server to check that your code can access it. This code should do the proper setup (i.e. it should send itself a mail). Guard this test with some global variable or System.property so you can enable/disable it at runtime.

Move the code to access the server into an isolated class.

Override this class in your tests. In the test, just check that the mail text is correct. If you get a bug report that accessing the server is broken, enable the "access the real server test" and check.

Aaron Digulla
+2  A: 

I would suggest to embed a pure Java IMAP/POP server in your test code.

For that, you have numerous possibilities, including:

  • Write your own IMAP mock using Javamail
  • Use Dwarf mail server
  • Use JMock to emulate the various interfaces of your mail server (after all, it must have an interface, no ?)
Riduidel
+1 for using a proper mocking framework. Just try to keep what you are testing as simple as possible. Don't want to have to worry about the intricacies of integrating with a different mail server.
Noel M
+4  A: 

Greenmail might be useful.

GreenMail is an open source, intuitive and easy-to-use test suite of email servers for testing purposes. Supports SMTP, POP3, IMAP with SSL socket support.

Bozho
This looks very good, they have code samples and Maven instructions, nice!
Hanno Fietz
Wasn't aware of that project. +1
Pascal Thivent
@Pascal Thivent - to be honest, I wasn't aware either. I'm just good at googling :) That's why I like SO - it makes you find very useful things.
Bozho
Actually, I guessed this was Google Fu. But this one is a very good finding.
Pascal Thivent