我正在尝试使用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"));
}
答案 0 :(得分:3)
你已经存根createMimeMessage
来返回模拟。据推测,这个模拟是传递给send
的东西;所以你的论证捕获者只是捕获模拟。模拟(getContentType()
和其他)上的每个方法都只返回null,因为你还没有存根它们。