模拟IMAP或POP服务器进行单元测试最简单的方法是什么? [英] What is the easiest way to mock an IMAP or POP server for unit tests?

查看:228
本文介绍了模拟IMAP或POP服务器进行单元测试最简单的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想单元测试从电子邮件收件箱中获取邮件的Java应用程序,非常像这个家伙。目前,我对我们公司真正的邮件服务器上的真实邮箱进行了单元测试,该邮箱很容易设置,但有以下缺点:

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:


  • 您必须在运行测试之前发送实际的电子邮件
  • 添加更多测试用例可能会很困难,例如因为您可能需要针对不同的安全策略进行测试

  • 测试取决于与邮件服务器的工作网络连接以及现有的邮件帐户,这些邮件帐户以不符合我的方式使开发和系统管理相结合。

我想在本地端口上启动一个IMAP服务器,该服务器根据存储在文件中的测试数据的测试数据来伪造一个收件箱。我可以想到以下方法:

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:


  • 运行套接字服务器并实现基本的IMAP子集

  • 使用用于构建邮件服务器的更高级别的库

  • 使用现有的电子邮件服务器实现,可以嵌入到我的测试中

我想避免第一个选项,这样看起来很简单,但是我从类似的经历中猜出,有一大堆工作正在等待进一步的进行。只是想想要测试安全连接等。同样,第二个选项似乎很多工作,但我还没有找到一个邮件服务器,这将允许第三个。

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.

如果重要,我在构建过程中使用Maven和TestNG。

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

推荐答案

Greenmail 可能很有用。


GreenMail是用于测试目的的开源,直观易用的电子邮件服务测试套件。
支持SMTP,POP3,支持SSL套接字的IMAP。

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.

这篇关于模拟IMAP或POP服务器进行单元测试最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆