开发者

What is the easiest way to mock an IMAP or POP server for unit tests? [duplicate]

开发者 https://www.devze.com 2023-01-05 14:12 出处:网络
This question already has answers here: Working with a Java Mail Server for Testing (5 answers) Closed 8 years ago.
This question already has answers here: Working with a Java Mail Server for Testing (5 answers) Closed 8 years ago.

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 Mave开发者_如何学Pythonn and TestNG during the build process.


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.


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.


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 ?)


The Mock-JavaMail project

I came across it when developing a plugin for Jenkins, and it's been a dream to use!

Just drop the dependency into your project, and you're ready to go (I'll let Kohsuke explain how to set it up and use it).

If you're impatient, here's a quick example of how it's used:

Example:

// Setup test: add mail to inbox
Mailbox tmp = Mailbox.get("foo@bar.com");
tmp.add(/* your javax.mail.Message */)
assertEquals 1, tmp.size()

// Connect to the inmemory mailbox using "imap"
Session session = Session.getInstance(System.getProperties(), null);
Store store = session.getStore('imap');
store.connect("bar.com","foo","anything");

// Check the mail exists!
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
assertEquals 1, inbox.getMessageCount()
store.close();
0

精彩评论

暂无评论...
验证码 换一张
取 消