我正在尝试使用 Mockito ArgumentCaptor 在我的方法中获取 mime 消息。当我取回捕获对象时,它的值为空。我对调试它很陌生,但 Mockito 用增强器包装了它,所以我看不到内容。这适用于我的方法中的对象。有人有想法吗?
这是我的样本测试。 msg 不为 null,但方法调用后返回 null。
@Test
public void testSendTemplatedMail() throws MessagingException, IOException {
Context ctx = new Context();
ctx.setVariable("name", "John Doe");
ctx.setVariable("subscriptionDate", new Date());
ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music"));
String templateName = "testEmailTemplateWithoutImage";
when(mailSenderMock.createMimeMessage()).thenReturn(mock(MimeMessage.class));
try {
mailUtils.sendTemplatedMail("John Doe", "[email protected] /cdn-cgi/l/email-protection",
"[email protected] /cdn-cgi/l/email-protection", "Hello",
templateName, ctx);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
ArgumentCaptor<MimeMessage> msg = ArgumentCaptor.forClass(MimeMessage.class);
verify(mailSenderMock, times(1)).createMimeMessage();
verify(mailSenderMock, times(1)).send(msg.capture());
verifyNoMoreInteractions(mailSenderMock);
System.out.println("Sample msg subject = " + msg);
System.out.println("Sample msg ctype = " + msg.getValue().getContentType());
System.out.println("Sample msg to = " + msg.getValue().getAllRecipients());
System.out.println("Sample msg sender = " + msg.getValue().getSender());
System.out.println("Sample msg from = " + msg.getValue().getFrom());
System.out.println("Sample msg content = " + msg.getValue().getContent());
// assertEquals("accountAlmostDone", mv.getViewName());
// assertEquals("NA", mv.getModel().get("activationCode"));
}
你已经断掉了createMimeMessage
返回一个模拟。据推测,这个模拟正在传递给send
;所以你的参数捕获器只是捕获模拟。模拟中的每个方法(getContentType()
和其他)只是返回 null,因为您还没有对它们进行存根。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)