8

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

1 に答える 1

4

createMimeMessageモックを返すためにスタブしました。おそらく、このモックが に渡されsendます。したがって、引数キャプターはモックをキャプチャしているだけです。モック (getContentType()およびその他) の各メソッドは、まだスタブ化していないため、単に null を返しています。

于 2013-08-11T19:36:08.927 に答える