gpt4 book ai didi

java - 模拟 MIMEMessage 的正确方法?

转载 作者:行者123 更新时间:2023-11-30 06:42:45 30 4
gpt4 key购买 nike

我想实际验证消息上设置的字段,如正文、主题、发件人等……我也尝试过做一个参数捕捉器,但没有用,当我调试代码时,消息是获取 javaMailSender.send(message) 行时始终显示空字段。

是否有模拟 MIMEMessage 的最佳实践?

//EmailService.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Slf4j
public class EmailService {
private JavaMailSender javaMailSender;

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


public void sendEmail() {
MimeMessage message = javaMailSender.createMimeMessage();
String body = "<html>\n" +
"<p> This the body </p>\n" +
"<br>\n";
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
mimeMessageHelper.setSubject("subject");
mimeMessageHelper.setTo("stack@overflow.com");
mimeMessageHelper.setFrom("overflow@stack.com");
mimeMessageHelper.setText(body, true);

javaMailSender.send(message);

} catch (MessagingException e) {
log.error("error");
}`enter code here`
}
}

下面是测试

//EmailServiceTest.java

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.springframework.mail.javamail.JavaMailSender;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;


public class EmailServiceTest {
private EmailService emailService;

@Mock
private JavaMailSender javaMailSender;

@Mock
private MimeMessage message;

@Before
public void setUp() throws IOException {
initMocks(this);

emailService = new EmailService(javaMailSender);
}

@Test
public void sendEmail_sendsEmailWithCorrectContent() throws MessagingException {
ArgumentCaptor<MimeMessage> mimeMessageArgumentCaptor = ArgumentCaptor.forClass(MimeMessage.class);
when(javaMailSender.createMimeMessage()).thenReturn(message);

doNothing().when(javaMailSender).send(message);

emailService.sendEmail();

verify(javaMailSender, times(1)).send((mimeMessageArgumentCaptor.capture()));
assertEquals("subject", mimeMessageArgumentCaptor.getValue().getSubject());
}

}

最佳答案

做这种测试通常比较容易using an integration test .电子邮件通常是使用 freemarker 等模板库构建的,因此集成测试对于验证您的模板是否正确也很有值(value)。

您可以使用 PowerMock 进行单元测试创建一个模拟 MimeMessageHelper,但 PowerMock 有时在跟上其他测试库的变化方面可能很慢,而且此类测试通常最终变得脆弱且值(value)不大。例如,您可以验证是否在 mimeMessageHelper 上调用了 setSubject(),但这是否会产生您真正想要测试的结果(主题设置在 消息)?

关于java - 模拟 MIMEMessage 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53091945/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com