Java 运行 Junit 电子邮件测试而不实际发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1027595/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:25:04  来源:igfitidea点击:

Running Junit Email Tests Without Emails Actually Going Out

javaunit-testingemailjunit

提问by stevedbrown

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?

我想在一些模型类上运行单元测试 (Junit),这些模型类通常会发送电子邮件确认事情发生了。是否有一个模拟电子邮件服务器可以与单元测试一起使用,让您确认您的运行尝试发送电子邮件而不实际发送电子邮件?

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.

这似乎很好,只是不确定我想写我自己的。电子邮件方面的应用程序堆栈是 Velocity+Spring,因此如果可以通过简单地更改 applicationContext.xml 文件来指向测试服务器,那就更好了。

采纳答案by Colin Pickard

Alternative answer: Dumbsteris a fake SMTP server designed for testing against. It's written in Java.

替代答案:Dumbster是一个伪造的 SMTP 服务器,用于测试。它是用Java编写的。

回答by Colin Pickard

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

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

回答by Jacob Mattison

I think the Mock JavaMailproject is what you want.

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

回答by Aaron Digulla

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.

我的解决方案是将邮件服务器包装在一个类中,该类包含所有配置选项并有一个send()方法。在我的测试中,我会模拟这个类并send()使用保存断言当前参数的东西覆盖。

To test that the mail service itself works, send yourself a mail locally. Try hMailif you're on Windows.

要测试邮件服务本身是否有效,请在本地给自己发送一封邮件。如果您使用的是 Windows,请尝试使用hMail

回答by Nick Holt

I assume you're using Javamail and the problem is that javax.mail.Sessionis final and therefore can't be mocked.

我假设您正在使用 Javamail 并且问题javax.mail.Session是最终的,因此不能被嘲笑。

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.

似乎其他人建议您简单地定义自己的“邮件会话”接口并创建一个使用 Javamail 的实现。在您的测试中,您只需注入一个模拟,而在“真实世界模式”中您注入 Javamail 实现。

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.

JMock 和 EasyMock 都将支持您可能想要对您发送的消息做出的所有断言,并且您的测试已完成。

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.

顺便说一句,我通常会尽量避免单元测试中的任何进程外调用 - 当您频繁运行测试套件时,它会杀死您,这通常会转化为它运行得更少,这就是代码库问题开始发生.

回答by salyh

You can try JavaMail Mock2 https://github.com/salyh/javamail-mock2

你可以试试 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.

它主要关注 IMAP/POP3,但也可以使用 SMTP Mock。它在 Maven 中心可用。

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

回答by Iker Aguayo

I would take a look to GreenMail which is alive (apparently Dumbster is dead) with a lot of functionality and good examples.

我会看看 GreenMail,它是活跃的(显然 Dumbster 已经死了),有很多功能和很好的例子。

http://www.icegreen.com/greenmail/

http://www.icegreen.com/greenmail/