0
我想知道如何使這個測試工作!以下是測試方法:正如您所看到的,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;
}
你能舉個例子嗎? – Phoenix
當然,看看這裏:http://fczaja.blogspot.co.il/2011/11/easymock-create-partial-mocks.html – Vitaliy
該鏈接不起作用 – Phoenix