当邮件包含模板时,如何在Springboot中编写Junit测试

发布于 2025-01-23 17:08:15 字数 1841 浏览 3 评论 0原文

我如何用模板为此邮件代码编写JUNIT,在这里我正在使用模板发送邮件 这是我的服务类方法:

    @Service
    public class ServiceImpl {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private Configuration configuration;
    @Value("${mail.send.to}")
    private String mailSendTo;
   
    public void sendMailOnEmailOptOut()
        throws MessagingException, IOException, TemplateException {
    String[] toAddressList = { mailSendTo };
    final String emailSubject = "email opt out testing";
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, 
    MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
            StandardCharsets.UTF_8.name());
    helper.setTo(toAddressList);
    StringWriter stringWriter = new StringWriter();
    Map<String, Object> model = new HashMap<>();
    model.put("user", "this is just for testing");
    configuration.getTemplate("email.template.html").process(model, stringWriter);
    String emailContent = stringWriter.getBuffer().toString();
    helper.setText(emailContent, true);
    helper.setSubject(emailSubject);
    javaMailSender.send(message);
}

mail_send_to value来自属性文件 这是电子邮件。

  <!doctype html>
 <html lang="en">
  <head>
  <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, 
 maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>email opt-out testing</title>
 </head>
<body>
<div style="margin-top: 10px">Greetings, <b>${user} </b></div>
<div>welcome ! this is just for testing functionality of email </br></br></div>
</body>
</html>

how i can write junit for this mail code with template , here i am sending mail with template
this is my Service class Method:

    @Service
    public class ServiceImpl {
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private Configuration configuration;
    @Value("${mail.send.to}")
    private String mailSendTo;
   
    public void sendMailOnEmailOptOut()
        throws MessagingException, IOException, TemplateException {
    String[] toAddressList = { mailSendTo };
    final String emailSubject = "email opt out testing";
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, 
    MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
            StandardCharsets.UTF_8.name());
    helper.setTo(toAddressList);
    StringWriter stringWriter = new StringWriter();
    Map<String, Object> model = new HashMap<>();
    model.put("user", "this is just for testing");
    configuration.getTemplate("email.template.html").process(model, stringWriter);
    String emailContent = stringWriter.getBuffer().toString();
    helper.setText(emailContent, true);
    helper.setSubject(emailSubject);
    javaMailSender.send(message);
}

mail_send_to value come from property file
and this is email.template.html is in src->main->resources template folder

  <!doctype html>
 <html lang="en">
  <head>
  <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, 
 maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>email opt-out testing</title>
 </head>
<body>
<div style="margin-top: 10px">Greetings, <b>${user} </b></div>
<div>welcome ! this is just for testing functionality of email </br></br></div>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

过期以后 2025-01-30 17:08:15

您可以使用Java系统存根为单元测试设置环境变量,请参阅此链接
https://www.baeldung.com/java-system-system-stubs-system-stubs

You can use Java System Stubs to set environment variables for your unit tests, see this link
https://www.baeldung.com/java-system-stubs

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