[英]How to mock javax.mail.Transport.send() method without actually sending email?
Code for sending email: 发送电子邮件的代码:
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
session.setDebug(true);
Transport.send(message);
I would like to test whether I am sending email correctly in mock way. 我想测试一下我是否以模拟方式正确发送电子邮件。 Please help!
请帮忙!
Here is an example test, using JMockit: 这是一个使用JMockit的示例测试:
@Test
public void checkEmailIsSent(@Mocked Transport emailTransport) {
// Call the code under test which sends email.
new Verifications() {{
Message msg;
Transport.send(msg = withCapture());
// perform regular asserts on the captured msg
}};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.