运行 Junit 电子邮件测试而无需实际发送电子邮件

发布于 2024-07-25 02:28:54 字数 234 浏览 5 评论 0原文

我想对一些模型类运行单元测试(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

御弟哥哥 2024-08-01 02:28:54

Dumbster 是一个假冒的 SMTP 服务器,旨在进行测试。 它是用 Java 编写的。

Dumbster is a fake SMTP server designed for testing against. It's written in Java.

£噩梦荏苒 2024-08-01 02:28:54

我认为 Mock JavaMail 项目就是您想要的。

I think the Mock JavaMail project is what you want.

剧终人散尽 2024-08-01 02:28:54

我会看一下 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/

痴梦一场 2024-08-01 02:28:54

我假设您使用的是 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.

百变从容 2024-08-01 02:28:54

您可以尝试 JavaMail Mock2 https://github.com/salyh/javamail-mock2

它主要专注于在 IMAP/POP3 上,但 SMTP 模拟也可用。 它在 Maven Central 中可用。

功能

  • 支持 imap、imaps、pop3、pop3s、smtp、smtps
  • 支持 POP3:转换为 POP3Folder、Folder.getUID(Message msg) 支持
  • IMAP:转换为 IMAPFolder、转换为 UIDFolder、子文件夹、-Folder.getMessagesByUID(... )、删除/重命名文件夹、附加消息
  • 支持 SMTP:Mock Transport.send()
  • 暂时不支持:IMAP 扩展,如 IDLE、CONDSTORE... 并转换为 POP3Message/IMAPMessage、存储侦听器

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

  • Support imap, imaps, pop3, pop3s, smtp, smtps
  • Supported for POP3: cast to POP3Folder, Folder.getUID(Message msg)
  • Supported for IMAP: cast to IMAPFolder, cast to UIDFolder, Subfolders, -Folder.getMessagesByUID(...), delete/rename folders, append messages
  • Support for SMTP: Mock Transport.send()
  • Unsupported for the moment: IMAP extensions like IDLE, CONDSTORE, ... and casts to POP3Message/IMAPMessage, store listeners
意犹 2024-08-01 02:28:54

Phil Haack 有一篇关于单元测试电子邮件发送的博客文章,其中他围绕免费软件邮件服务器编写了解决方案。

Phil Haack has a blog post about unit testing email sending, with a solution he coded around a freeware mail server.

憧憬巴黎街头的黎明 2024-08-01 02:28:54

我的解决方案是将邮件服务器包装在一个类中,该类接受所有配置选项并具有 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 override send() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文