Back to Repositories

Testing Email Service Implementation in spring-boot-demo

This test suite validates email functionality in a Spring Boot application, covering various types of email sending capabilities including simple text, HTML templates, attachments, and static resources. The tests demonstrate comprehensive integration with Spring’s email and template services.

Test Coverage Overview

The test suite provides extensive coverage of email service functionality in Spring Boot.

Key areas tested include:
  • Simple text email sending
  • HTML template-based emails with dynamic content
  • Custom template directory handling
  • Attachment email capabilities
  • Static resource embedding in emails

Implementation Analysis

The testing approach utilizes JUnit with Spring Boot’s testing framework for email service validation. Tests leverage Spring’s dependency injection for accessing MailService and TemplateEngine components.

Notable patterns include:
  • Template context manipulation for dynamic content
  • Resource handling for attachments and static content
  • Exception handling for messaging errors

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Spring Boot Test annotations
  • Thymeleaf template engine
  • SpringResourceTemplateResolver for custom templates
  • HuTool’s ResourceUtil for file handling
  • JavaMail API for messaging

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through organized and comprehensive test methods.

Notable practices include:
  • Clear test method naming and documentation
  • Proper separation of different email testing scenarios
  • Thorough exception handling
  • Efficient resource management
  • Modular template processing approach

xkcoding/spring-boot-demo

demo-email/src/test/java/com/xkcoding/email/service/MailServiceTest.java

            
package com.xkcoding.email.service;

import cn.hutool.core.io.resource.ResourceUtil;
import com.xkcoding.email.SpringBootDemoEmailApplicationTests;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;

import javax.mail.MessagingException;
import java.net.URL;

/**
 * <p>
 * 邮件测试
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-21 13:49
 */
public class MailServiceTest extends SpringBootDemoEmailApplicationTests {
    @Autowired
    private MailService mailService;
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private ApplicationContext context;

    /**
     * 测试简单邮件
     */
    @Test
    public void sendSimpleMail() {
        mailService.sendSimpleMail("[email protected]", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
    }

    /**
     * 测试HTML邮件
     *
     * @throws MessagingException 邮件异常
     */
    @Test
    public void sendHtmlMail() throws MessagingException {
        Context context = new Context();
        context.setVariable("project", "Spring Boot Demo");
        context.setVariable("author", "Yangkai.Shen");
        context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");

        String emailTemplate = templateEngine.process("welcome", context);
        mailService.sendHtmlMail("[email protected]", "这是一封模板HTML邮件", emailTemplate);
    }

    /**
     * 测试HTML邮件,自定义模板目录
     *
     * @throws MessagingException 邮件异常
     */
    @Test
    public void sendHtmlMail2() throws MessagingException {

        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(context);
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/email/");
        templateResolver.setSuffix(".html");

        templateEngine.setTemplateResolver(templateResolver);

        Context context = new Context();
        context.setVariable("project", "Spring Boot Demo");
        context.setVariable("author", "Yangkai.Shen");
        context.setVariable("url", "https://github.com/xkcoding/spring-boot-demo");

        String emailTemplate = templateEngine.process("test", context);
        mailService.sendHtmlMail("[email protected]", "这是一封模板HTML邮件", emailTemplate);
    }

    /**
     * 测试附件邮件
     *
     * @throws MessagingException 邮件异常
     */
    @Test
    public void sendAttachmentsMail() throws MessagingException {
        URL resource = ResourceUtil.getResource("static/xkcoding.png");
        mailService.sendAttachmentsMail("[email protected]", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", resource.getPath());
    }

    /**
     * 测试静态资源邮件
     *
     * @throws MessagingException 邮件异常
     */
    @Test
    public void sendResourceMail() throws MessagingException {
        String rscId = "xkcoding";
        String content = "<html><body>这是带静态资源的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
        URL resource = ResourceUtil.getResource("static/xkcoding.png");
        mailService.sendResourceMail("[email protected]", "这是一封带静态资源的邮件", content, resource.getPath(), rscId);
    }
}