I have a routine that does the following:
public void onEvent(List<Dto> pendingData)
{
eventDao.saveBatch(pendingData);
pendingData.clear();
}
In my test of the routine I use @Mock for eventDao:
@Mock
private EventDAO eventDao;
@Test
public void onEvent() {
// some preparations
handler.onEvent(testEvents);
// eventsExpectedToBePassed is the same as testEvents but it is an
// independent object populated separately
verify(eventDao, times(1)).saveBatch(eventsExpectedToBePassed);
}
verify fails with the information that the routine was called with empty array. This likely happens because Mockito simply stores reference to the array passed to the mock, and as you can see from the onEvent code, that array gets cleared in course of later execution. If I peform a copy by calling instead
eventDao.saveBatch(new ArrayList(pendingData));
then test passes OK. However, I like to avoid that since this is an extra operation that I do not need.
How I can either tell Mockito to save copy of passed array, not array itself, so when array is cleared, in the test code I have it retained for assertion?