springBoot 写单元测试

springBoot 单元测试

首先需要引用test jar 推荐使用maven 管理项目

具体坐标如下:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

还要引用对应的 parend pom

package springBootQuartz;

import java.io.File;
import java.util.HashMap;

import javax.mail.MessagingException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.iflytek.ifi.quartz.Application;
import com.iflytek.ifi.quartz.util.SendMailUtil;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)// 程序的启动类
@WebAppConfiguration //标识是web项目
public class TestMail {

    @Autowired
    private SendMailUtil sendMailUtil;


    public void sendMail() throws MessagingException{
        sendMailUtil.sendHtmlMail("[email protected]", "普通邮件标题","普通邮件内容");

    }
    public void sendHtmlMail() throws MessagingException{
        sendMailUtil.sendHtmlMail("[email protected]", "带html格式的邮件标题","<h1>紧急通知</h1><div style='color:red;'>放假三天</div>");

    }

    public void sendAttachmentMail() throws MessagingException{
        sendMailUtil.sendAttachmentMail("[email protected]", "有附件的邮件", "放假三天", new File("D:/eclipse_git.png"));
    }

    public void sendInlineMail() throws MessagingException{
        sendMailUtil.sendInlineMail("[email protected]", "内容有静态资源", "大家好 ,这是我的eclipse配置git信息  ", new File("D:/eclipse_git.png"));
    }
    @Test
    public void sendTemplateMail() throws Exception{
        HashMap<String, Object> model = new HashMap<String, Object>();
        model.put("username", "zhsj");
        model.put("age", "18");
        sendMailUtil.sendTemplateMail("[email protected]","这是一封模板邮件",model);
    }
}

猜你喜欢

转载自blog.csdn.net/forever_insist/article/details/80280296