Joe*_*Joe 5 junit mime unit-testing mockito
我正在尝试使用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"));
}
Run Code Online (Sandbox Code Playgroud)
你已经成功createMimeMessage返回了一个模拟。据推测,这个模拟就是被传递给的send;所以你的参数捕获器只是捕获模拟。模拟中的每个方法(getContentType()以及其他方法)都只是返回 null,因为您还没有对它们进行存根处理。
| 归档时间: |
|
| 查看次数: |
4981 次 |
| 最近记录: |