0

I'm wondering how to make this test work! The below is the test method: As you can see the correlationId and docFamilyUUid is randomly generated by UUID class. Basically the getDocInfo first calls findAllByDocId on the mockDao and if that returns null it creates a docInfo object to be saved to the database.

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
  • 8,695
  • 16
  • 55
  • 88

1 Answers1

1

You should use partial mocking to mock out only the createDocInfo method. This way you can return whatever you want from there, specifically an instance of DocInfo with an id that you choose. This way you can make the necessary assertions on the behavior of getDocInfo (like making sure that an instance with the same id is passed to save and add).

You can read about partial mocking here. You did not state which version of EasyMock you are using so I am assuming it is not too old.

Vitaliy
  • 8,044
  • 7
  • 38
  • 66