gpt4 book ai didi

java - Mockito 与 JavaMailSender

转载 作者:行者123 更新时间:2023-12-01 17:50:05 26 4
gpt4 key购买 nike

我的测试失败,这是我的测试类。

public class EmailGeneratorServiceTests {

@Mock
JavaMailSender javaMailSender;

EmailGeneratorService emailGeneratorService;

@Before
public void before() {
MockitoAnnotations.initMocks(this);
emailGeneratorService = new EmailGeneratorService(javaMailSender);
}


@Test
public void generateAndSendMessageSuccess() throws MessagingException, IOException {
String email = "test@test.com";
String body = "Some contents.";
String subject = "Some subject";
emailGeneratorService.generateAndSendMessage(email, body, subject);
List<Message> inbox = Mailbox.get(email);
//inbox.size === 0
assertTrue(inbox.size() == 1);
assertEquals(subject, inbox.get(0).getSubject());
assertEquals(body, inbox.get(0).getContent());
}
}

当我调用 inbox.size() 时,我得到 0,但预期为 1。服务器是 smtp。

这是 EmailGeneratorService。

@Service
public class EmailGeneratorService {
SimpleMailMessage simpleMailMessage;
JavaMailSender javaMailSender;

@Autowired
public EmailGeneratorService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
this.simpleMailMessage = new SimpleMailMessage();
}

public void generateAndSendMessage (String email, String messageBody, String subject) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(email);
simpleMailMessage.setText(messageBody);
simpleMailMessage.setSubject(subject);
javaMailSender.send(simpleMailMessage);
}
}

请写出错误所在并解释错误所在。

最佳答案

您应该捕获JavaMailSender来获取参数并断言它。

ArgumentCaptor<SimpleMailMessage> emailCaptor = ArgumentCaptor.forClass(SimpleMailMessage .class);
verify(javaMailSender, times(1)).send(emailCaptor.capture());

List<SimpleMailMessage> actualList = emailCaptor.getAllValues();
assertTrue(actualList.size() == 1);
assertEquals(subject, actualList.get(0).getSubject());
assertEquals(body, actualList.get(0).getContent());

和用户ErrorCollector断言多个值,在@Before方法之前定义它,如下所示:

@Rule
public ErrorCollector collector= new ErrorCollector();

,并在断言中像这样使用它:

collector.checkThat(getResult(), equalTo("ERROR!"));

你的测试应该是这样的:

public class EmailGeneratorServiceTests {

private static final String EMAIL = "test@test.com";
private static final String BODY = "Some contents.";
private static final String SUBJECT = "Some subject";

@Mock
private JavaMailSender javaMailSender;

private EmailGeneratorService emailGeneratorService;

@Rule
public ErrorCollector collector = new ErrorCollecto();

@Before
public void before() {
MockitoAnnotations.initMocks(this);
emailGeneratorService = new EmailGeneratorService(javaMailSender);
}


@Test
public void generateAndSendMessageSuccess() throws Exception {
// Arrange
emailGeneratorService.generateAndSendMessage(email, body, subject);

// Act
Mailbox.get(email);

// Assert
ArgumentCaptor<SimpleMailMessage> emailCaptor =
ArgumentCaptor.forClass(SimpleMailMessage .class);
verify(javaMailSender, times(1)).send(emailCaptor.capture());

List<SimpleMailMessage> actualList = emailCaptor.getAllValues();
collector.checkThat(actualList.size(), equalTo(1));
collector.checkThat(actualList.get(0).getSubject(), equalTo(SUBJECT));
collector.checkThat(actualList.get(0).getContent(), equalTo(CONTENT));
}
}

关于java - Mockito 与 JavaMailSender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60813735/

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