我想知道如何让这个测试工作!以下是测试方法:正如您所看到的,correlationId和docFamilyUUid是由UUID类随机生成的。基本上,getDocInfo首先在mockDao上调用findAllByDocId,如果返回null,则会创建一个docInfo对象以保存到数据库中。
void testGetDocInfo()
{
def String docId = 'I19292800fe1911e0a849005056932b99'
def List<DocInfo> expectedResult = [expectedDocInfo]
expect(mockDocInfoDao.findAllByDocId(docId)).andReturn(null)
mockDocInfoDao.save(new DocInfo())
replay(mockDocInfoDao)
docInfoServiceImpl.getDocInfo(docId)
verify(mockDocInfoDao)
}
///////// DocInfoServiceClass
public final DocInfo createDocInfo(final String docId)
{
final DocInfo docInfo = new DocInfo();
docInfo.setId(docId);
docInfo.setDocFamilyUuid(UUIDGenerator.getInstance().getUuidAsString());
docInfo.setCorrelationId(UUIDGenerator.getInstance().getUuidAsString());
return docInfo;
}
/**
* @param docId is the document Id
* @return the list of DocInfo objects for a particular docId
*/
public final List<DocInfo> findAllByDocId(final String docId)
{
return docInfoDao.findAllByDocId(docId);
}
/**
* @param docId is the document Id
* @return the list of DocInfo objects for a particular docId
*/
public final List<DocInfo> getDocInfo(final String docId)
{
List<DocInfo> docInfoList = null;
docInfoList = docInfoDao.findAllByDocId(docId);
if (docInfoList == null)
{
docInfoList = new ArrayList<DocInfo>();
DocInfo docInfo = createDocInfo(docId);
docInfoDao.save(docInfo);
docInfoList.add(docInfo);
}
return docInfoList;
}
答案 0 :(得分:1)
您应该使用部分模拟来仅模拟createDocInfo方法。这样,您可以从那里返回任何您想要的内容,特别是具有您选择的ID的DocInfo实例。 这样你就可以对getDocInfo的行为做出必要的断言(比如确保传递一个具有相同id的实例来保存和添加)。
您可以阅读部分模拟here。 你没有说明你使用的是哪个版本的EasyMock,所以我认为它不是太旧了。