java 模拟 IMAP 或 POP 服务器进行单元测试的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3180192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest way to mock an IMAP or POP server for unit tests?
提问by Hanno Fietz
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:
我想对一个从电子邮件收件箱中获取邮件的 Java 应用程序进行单元测试,就像这个家伙一样。目前,我在我们公司的真实邮件服务器上针对真实邮箱运行单元测试,该服务器易于设置,但存在以下缺点:
- 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:
我想在本地端口上启动一个 IMAP 服务器,它根据存储在测试类旁边的文件中的测试数据伪造收件箱。我可以想到以下方法:
- 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
- 运行一个套接字服务器并实现一个基本的 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.
我想避免第一个选项,它看起来很简单,但我从类似的经验中猜测,还有很长的工作等待着更远的路。想想想要测试安全连接等。同样,第二个选项似乎有很多工作,但我还没有找到允许第三个选项的邮件服务器。
If it matters, I'm using Maven and TestNG during the build process.
如果重要的话,我在构建过程中使用 Maven 和 TestNG。
回答by Bozho
回答by Aaron Digulla
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.propertyso you can enable/disable it at runtime.
编写一个依赖于现有邮件服务器的测试来检查您的代码是否可以访问它。这段代码应该做正确的设置(即它应该给自己发送一封邮件)。使用一些全局变量保护这个测试,或者System.property你可以在运行时启用/禁用它。
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.
在您的测试中覆盖此类。在测试中,只需检查邮件文本是否正确。如果您收到错误报告说访问服务器已损坏,请启用“访问真实服务器测试”并检查。
回答by Riduidel
I would suggest to embed a pure Java IMAP/POP server in your test code.
我建议在您的测试代码中嵌入一个纯 Java IMAP/POP 服务器。
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 ?)
- 使用 Javamail 编写您自己的 IMAP 模拟
- 使用矮人邮件服务器
- 使用 JMock 模拟你的邮件服务器的各种接口(毕竟它必须有一个接口,不是吗?)
回答by Nick Grealy
The Mock-JavaMailproject
该模拟,JavaMail的项目
I came across it when developing a plugin for Jenkins, and it's been a dream to use!
我在为 Jenkins 开发插件时遇到了它,并且一直梦想使用它!
Just drop the dependencyinto your project, and you're ready to go (I'll let Kohsuke explainhow to set it up and use it).
只需将依赖项放入您的项目中,您就可以开始了(我将让Kohsuke 解释如何设置和使用它)。
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("[email protected]");
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();

