Expectation failure on verify: load(com.disney.datg.services.directorywatcher.beans.WatchFolderDataVO@65a601b0, 5): expected: 1, actual: 0

StackOverflow https://stackoverflow.com/questions/22499101

  •  17-06-2023
  •  | 
  •  

Question

final WatchFolderDataVO watchFolderDataVO = new WatchFolderDataVO();
final SessionFactory sessionFactoryMock = EasyMock.createNiceMock(SessionFactory.class);
final Session sessionMock = EasyMock.createNiceMock(Session.class);

EasyMock.expect(sessionFactoryMock.openSession()).andReturn(sessionMock).anyTimes();
sessionMock.load(watchFolderDataVO, 5);
EasyMock.expectLastCall();
EasyMock.replay(sessionFactoryMock);
watchFolderDao.setSessionFactory(sessionFactoryMock);
EasyMock.replay(sessionMock);
watchFolderDao.updateWatchFolderDataTable(pollingTime, xmlOnly, 5, fileWatcherDetails);
EasyMock.verify(sessionFactoryMock);
EasyMock.verify(sessionMock);

when I am calling updateWatchFolderDataTable, I am getting the following error:

Expectation failure on verify:     
load(com.disney.datg.services.directorywatcher.beans.WatchFolderDataVO@65a601b0, 5): expected: 1, actual: 0

because of the new instance of the WatchFolderDataVO being created in the method....

final WatchFolderDataVO watchFolderDataVO = new WatchFolderDataVO();
        try {
            session = getSession();
            session.load(watchFolderDataVO, watchFolderDataId);
Was it helpful?

Solution

You obviously understand that the failure is because of the different instance of WatchFolderDataVO, so I won't go on too much about that.

There are a couple of ways to work with this, assuming you don't want to refactor to not create a new instance of WatchFolderDataVO

  • Provide a matcher to the expectation.

I would suggest the EasyMock.isA() matcher. This will make sure that the method is called with an object of the type you provide. Keep in mind, if you use one matchers, then you must use matchers for all of the parameters to the method. So your expectation would become:

sessionMock.load(EasyMock.isA(WatchFolderDataVO.class), EasyMock.eq(5));
EasyMock.expectLastCall();

Keep in mind that this will not handle nulls well. If the method is called with a null WatchFolderDataVO then EasyMock will just fall over in a big heap.

  • Use the capture() method and a Capture

Captures allow you to grab the object that the method is called with and make assertions on it. This can be useful if you do need to make sure that the method is called with a different (or specific) object or if you need to ensure a given state is set up before the method is called. Again, the capture() method is a matcher, so you must use matchers for all of the parameters to the method, so your expectation would look like this:

Capture<WatchFolderDataVO> watchFolderDataCapture = new Capture<WatchFolderDataVO>();
sessionMock.load(EasyMock.capture(watchFolderDataCapture ), EasyMock.eq(5));
EasyMock.expectLastCall();
//The rest of your test
//You can then do tests like the following
WatchFolderDataVO capturedValue = watchFolderDataCapture .getValue();
assertNotSame(capturedValue, watchFolderDataVO);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top