Java 使用伪smtp服务器生成MailSendException
我需要测试一些使用SpringMailSender实例发送电子邮件批处理的组件。当提供的目标地址格式不正确时,方法“send”抛出MailSendException,如myAddressgmail.com(请注意,我并不是假装我的smtp服务器知道目标主机中是否确实存在该地址,只是在谈论结构)。事实上,这就是使用真正的smtp服务器时发生的情况Java 使用伪smtp服务器生成MailSendException,java,spring,unit-testing,jakarta-mail,Java,Spring,Unit Testing,Jakarta Mail,我需要测试一些使用SpringMailSender实例发送电子邮件批处理的组件。当提供的目标地址格式不正确时,方法“send”抛出MailSendException,如myAddressgmail.com(请注意,我并不是假装我的smtp服务器知道目标主机中是否确实存在该地址,只是在谈论结构)。事实上,这就是使用真正的smtp服务器时发生的情况 为了模拟smtp服务器,我尝试了Dumbster和GreenMail,这里出现了问题。这些假冒的smtp服务器不会检查地址是否格式正确,因此不会引发Ma
为了模拟smtp服务器,我尝试了Dumbster和GreenMail,这里出现了问题。这些假冒的smtp服务器不会检查地址是否格式正确,因此不会引发MailSendException。我需要抛出此异常以测试异常处理。使用类似或的模拟库 然后,您可以执行以下操作:
MailSender mockSender = mock(MailSender.class);
YourSMTPServer server = new YourSMTPServer(mockSender);
when(mockSender.send(...)).thenThrow(new MailSendException(...));
// then invoke the method on your server that calls MailSender.send() and
// assert it does what you want it to do when an exception is encountered
使用类似或的模拟库 然后,您可以执行以下操作:
MailSender mockSender = mock(MailSender.class);
YourSMTPServer server = new YourSMTPServer(mockSender);
when(mockSender.send(...)).thenThrow(new MailSendException(...));
// then invoke the method on your server that calls MailSender.send() and
// assert it does what you want it to do when an exception is encountered
您可以使用mailnest.io进行电子邮件测试。在平台中有一个错误模式,您可以打开该模式模拟服务器的错误响应。您可以使用mailnest.io进行电子邮件测试。在平台中有一种错误模式,您可以打开它来模拟服务器的错误响应。如果Dumbster看到某一类型的地址,修改它以返回错误应该相当容易。请参阅我的答案。如果Dumbster看到某一类型的地址,修改它以返回错误应该相当容易。请参阅我的答案我错过了这个方法。谢谢我错过了这个方法。谢谢