我想知道如何使这个测试工作!下面是测试方法: 如您所见,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)
}
///////// DocInfoService类
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;
}