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)
    }

///////// 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;
    }
4

1 回答 1

1

您应该使用部分模拟来仅模拟 createDocInfo 方法。这样你就可以从那里返回任何你想要的东西,特别是一个带有你选择的 id 的 DocInfo 实例。这样,您可以对 getDocInfo 的行为做出必要的断言(例如确保将具有相同 id 的实例传递给保存和添加)。

您可以在此处阅读有关部分模拟的信息。您没有说明您使用的是哪个版本的 EasyMock,所以我假设它不是太旧。

于 2012-09-19T17:19:13.563 回答