Working with a Java Mail Server for Testing

[亡魂溺海] 提交于 2019-11-30 13:05:07

There is also very simple in use GreenMail which was designer as a mail server for automatic "unit" tests.

From projects web page (probably there are some others tools with sending/receiving functionality nowadays):

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. GreenMail also provides a JBoss GreenMail Service. GreenMail is the fist and only library that offers a test framework for both receiving and retrieving emails from Java.

I've used both Dumbster and SubEthaSmtp in unit tests before to test code that sends email.

I found Dumbster to be far easier to work with.

Take a look at JES, seems to do what you want.

Dumbster: Fast to setup! But can't handle mail attachments. There only strings at the end of the body and have to be parsed separately.

So now I'm trying another framework

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!