我正在尝试使用Mockito ArgumentCaptor在我的方法中获取mime消息。当我取回捕获对象时,它的值为null。我试着调试它,但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", "john.doe@bbc.com",
"no-reply@leanvelocitylabs.com", "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
以返回mock。据推测,这个mock就是传递给send
的内容;所以你的论点捕获者只是在捕捉嘲笑。mock上的每个方法(getContentType()
和其他方法)都只是返回null,因为您还没有对它们进行存根处理。