Mockito ArgumentCaptor返回Null [英] Mockito ArgumentCaptor is Returning Null

查看:177
本文介绍了Mockito ArgumentCaptor返回Null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Mockito ArgumentCaptor to get the mime message in my method. When I get the capture object back its values are null. I tyro to debug it, but Mockito wraps it with an enhancer, so I cannot see the contents. That applies to the objects in my method. Does anyone have an idea?

这是我的样本测试. msg不是null,但此后调用该方法将返回null.

Here is my sample test. msg is not null, but the method calls after that return nulls.

@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进行存根以返回模拟.大概,这个模拟就是传递给send的东西.因此,您的参数捕获者只是捕获了模拟.模拟中的每个方法(getContentType()和其他方法)都只是返回null,因为您尚未对它们进行存根处理.

You've stubbed createMimeMessage to return a mock. Presumably, this mock is what is being passed to send; so your argument captor is just capturing the mock. Each of the methods on the mock (getContentType() and the others) is just returning null, because you haven't stubbed them yet.

这篇关于Mockito ArgumentCaptor返回Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆