我想对以下方法进行单元测试
public void addRecord(Record record)
{
Myclass newObj = new Mycalss();
// It creates newObj object, set some values using record object.
// and it adds the newObj in daatbase.
dataReqDao.persist(newObj);
}
我曾经嘲笑过dataReqDao.persist
方法,但如何验证是否将正确的值复制到 newObj 对象中?我想获取 newObj 对象。
I think thenAnswer
将是检索 newObj 即方法参数的适当方法,但不知道如何使用返回 void 的方法。
Update:
我试过
doAnswer(new Answer<Myclass>() {
public Myclass answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
return (Myclass)args[0];
}
}).when(dataReqDao.persist(any(Myclass.class)));
EDIT:
应该是(谢谢大卫)
doAnswer(new Answer<Myclass>() {
public Myclass answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
return (Myclass)args[0];
}
}).when(dataReqDao).persist(any(Myclass.class));
您可以创建一个自定义参数匹配器 http://mockito.googlecode.com/svn/tags/1.8.0/javadoc/org/mockito/Mockito.html#3这将检查该对象的字段,或者使用争论捕获者 http://mockito.googlecode.com/svn/tags/1.8.0/javadoc/org/mockito/Mockito.html#15捕获物体以进行进一步检查。
例如如下:
ArgumentCaptor<Myclass> c = ArgumentCaptor.forClass(Myclass.class);
verify(dateReqDao).persist(c.capture());
Myclass newObj = c.getValue();
... // Validate newObj
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)