如何测试JavaMailSender?

时间:2017-04-13 09:07:24

标签: java email spring-boot junit

我没什么问题。我创建了MailService来发送邮件。当我运行程序时,它的工作原理。我在resources/application.properties发送的所有电子邮件属性。我正在使用spring-boot-starter-mail

@Service
public class MailService {
    private JavaMailSender javaMailSender;

    @Autowired
    public MailService(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendMail(String subject, String messageContent, String recipient)
            throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setTo(recipient);
        messageHelper.setSubject(subject);
        messageHelper.setText(messageContent);
        javaMailSender.send(mimeMessage);
    }
}

但我不知道如何为它创建测试。我试过这样的东西,我使用org.jvnet.mock-javamail:mock-javamail,但它不起作用:

public class MailServiceTest {
    private MailService mailService;

    @Mock
    private JavaMailSender javaMailSender;


    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mailService = new MailService(javaMailSender);
        Mailbox.clearAll();
    }

    @Test
    public void sendMailTest() throws MessagingException, IOException {
        String subject = "Some subject";
        String body = "Some contents.";
        String recipient = "test@test.com";

        mailService.sendMail(subject, body, recipient);
        List<Message> inbox = Mailbox.get(recipient);
        assertTrue(inbox.size() == 1);
        assertEquals(subject, inbox.get(0).getSubject());
        assertEquals(body, inbox.get(0).getContent());
    }
}

4 个答案:

答案 0 :(得分:4)

我认为你正在进行单元测试/嘲弄错误。您似乎想要创建单元测试;但期望集成测试的结果。

您可以在此处进行单元测试:

  • 提供模拟 JavaMailSender(就像您已经拥有)
  • 稍后在该模拟中使用验证

换句话说:你正在嘲笑实际发送邮件。因此,你不能指望邮件会出现在某个地方!

唯一可能的事情:确保您希望看到实际发生的方法调用。但这基本上会导致您编写一个简单的&#34;重新实现&#34;您的生产代码使用验证调用。这不太有帮助。

您可能应该在此处查看真实 集成测试。发送真实电子邮件;如果邮件显示在那里,请查看真实的收件箱。

答案 1 :(得分:1)

您可以使用以下方法测试MimeMessage的形成方式:

public class EmailServiceTest {

    private EmailServiceImpl emailServiceImpl;

    private JavaMailSender javaMailSender;

    private MimeMessage mimeMessage;

    @Before
    public void before() {
        mimeMessage = new MimeMessage((Session)null);
        javaMailSender = mock(JavaMailSender.class);
        when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
        emailServiceImpl = new EmailService(javaMailSender);
    }

    @Test
    public void emailTest() {
        String recipient = "example@example.com"
        EmailRequest request = new EmailRequest();
        request.setRecipient(recipient);
        emailServiceImpl.send(request);
        assertEquals(recipient, mimeMessage.getRecipients(RecipientType.TO)[0].toString());
    }
}

答案 2 :(得分:0)

我使用Mockito来模拟JavaMailSender bean,并在仅使用 test 配置文件运行的@Configuration注释的类中实例化它:

import static org.mockito.Mockito.*;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import org.springframework.context.annotation.*;
import org.springframework.mail.javamail.JavaMailSender;

@Profile("test")
@Configuration
public class EmailServiceTest {
    @Bean
    @Primary
    public JavaMailSender javaMailSender() {
        JavaMailSender javaMailSender = mock(JavaMailSender.class);
        when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null));
        return javaMailSender;
    }
}

答案 3 :(得分:0)

您可以使用greenmail http://www.icegreen.com/greenmail代替模拟电子邮件服务器

@Test
public void test_MyMailSend() {
    GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
    try {
        greenMail.start();
        //your class method call goes her 
        assertTrue(greenMail.waitForIncomingEmail(5000, 1));
        //Retrieve using GreenMail API
        Message[] messages = greenMail.getReceivedMessages();
        final String subject = messages[0].getSubject();
        final String msgContent = messages[0].getContent().toString();
        assertEquals(1, messages.length);
        assertEquals(subject, MyClass.SUPPORT_SUBJECT);
        assertEquals(messages[0].getAllRecipients()[0].toString(), MyClass.SUPPORT_ADDRESS);
        assertEquals(messages[0].getFrom()[0].toString(), MyClass.FROM_EMAIL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        greenMail.stop();
    }
}