运行 Junit 电子邮件测试而无需实际发送电子邮件
我想对一些模型类运行单元测试(Junit),这些模型类通常会发送电子邮件确认事情发生了。 是否有一个模拟电子邮件服务器可以与单元测试一起使用,它可以让您确认您的运行尝试发送电子邮件而无需实际发送电子邮件?
这似乎是一个很好的选择,只是不确定我想写自己的。 电子邮件方面的应用程序堆栈是 Velocity+Spring,因此如果可以通过简单地更改 applicationContext.xml 文件来指向测试服务器,那就更好了。
I want to run unit tests (Junit) on some model classes that typically will send emails confirming that things happened. Is there a mock email server that you can use with unit tests that will let you confirmation that your run tried to send an email without actually sending the emails out?
This seems like it would be a nice to have, just not sure I want to write my own. The app stack for the emailing aspect is Velocity+Spring, so it would be preferable if the test server can be pointed at by simply changing the applicationContext.xml file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Dumbster 是一个假冒的 SMTP 服务器,旨在进行测试。 它是用 Java 编写的。
Dumbster is a fake SMTP server designed for testing against. It's written in Java.
我认为 Mock JavaMail 项目就是您想要的。
I think the Mock JavaMail project is what you want.
我会看一下 GreenMail,它还活着(显然 Dumbster 已经死了),有很多功能和很好的例子。
http://www.icegreen.com/greenmail/
I would take a look to GreenMail which is alive (apparently Dumbster is dead) with a lot of functionality and good examples.
http://www.icegreen.com/greenmail/
我假设您使用的是 Javamail,问题是 javax.mail.Session 是最终的,因此不能被嘲笑。
似乎其他人建议您简单地定义自己的“邮件会话”接口并创建一个使用 Javamail 的实现。 然后,在您的测试中,您只需注入模拟,而在“真实世界模式”中则注入 Javamail 实现。
JMock 和 EasyMock 都将支持您可能想要对正在发送的消息做出的所有断言,并且测试已完成。
顺便说一句,我通常会尝试避免单元测试中的任何进程外调用 - 当您频繁运行测试套件时,它会杀死您,这通常意味着它运行较少,并且代码库问题开始发生。
I assume you're using Javamail and the problem is that
javax.mail.Session
is final and therefore can't be mocked.Seems like others have suggested you simply define your own 'mail session' interface and create an implementation that uses Javamail. In your tests you then simply inject a mock while in 'real-world-mode' you inject the Javamail implementation.
Both JMock and EasyMock will support all the assertions you might want to make on the message you are sending and you testing is complete.
As an aside, I generally try to avoid any out-of-process calls from within unit tests - it kills you when you're running the test suite frequently, which normally translates into it being run less and that's code base issues start to occur.
您可以尝试 JavaMail Mock2 https://github.com/salyh/javamail-mock2
它主要专注于在 IMAP/POP3 上,但 SMTP 模拟也可用。 它在 Maven Central 中可用。
功能
You can try JavaMail Mock2 https://github.com/salyh/javamail-mock2
Its primarily focused on IMAP/POP3 but SMTP Mock is also available. Its available in maven central.
Features
Phil Haack 有一篇关于单元测试电子邮件发送的博客文章,其中他围绕免费软件邮件服务器编写了解决方案。
Phil Haack has a blog post about unit testing email sending, with a solution he coded around a freeware mail server.
我的解决方案是将邮件服务器包装在一个类中,该类接受所有配置选项并具有
send()
方法。 在我的测试中,我会模拟此类并使用保存断言当前参数的内容覆盖send()
。要测试邮件服务本身是否正常工作,请在本地给自己发送一封邮件。 如果您使用的是 Windows,请尝试 hMail。
My solution was to wrap the mail server in a class which takes all the config options and has a
send()
method. In my tests, I'd mock this class and overridesend()
with something that saves the current parameters for the assert.To test that the mail service itself works, send yourself a mail locally. Try hMail if you're on Windows.