8

我正在尝试使用 Mockito ArgumentCaptor 在我的方法中获取 mime 消息。当我取回捕获对象时,它的值为空。我很想调试它,但是 Mockito 用增强器包装了它,所以我看不到内容。这适用于我的方法中的对象。有人有想法吗?

这是我的示例测试。msg 不为空,但之后调用的方法返回空值。

@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"));
}
4

1 回答 1

4

你已经存根createMimeMessage返回一个模拟。据推测,这个模拟是传递给send; 所以你的论点捕捉者只是在捕捉模拟。模拟(getContentType()和其他)上的每个方法都只是返回 null,因为您还没有对它们进行存根。

于 2013-08-11T19:36:08.927 回答