Consider following stub :
class InformationProvider{
private FileAccessor; //custom interface, works like readable DAO, but on files.
public List<Information> getInformation (Collection<String> identifiers){
List<Information> infoList = getInformationFromMemory(identifiers);
List<String> remaining = getListofIdsNotInMemory(identifiers, infoList); //looks up which IDs need to be searched in files.
infoList.addAll(fileAccessor.getInformation(remaining));
return infoList;
}
}
I am writing a unit test for the provider. My idea was like following:
class InformationProviderTest{
InformationProvider infoprovider = new InformationProvider();
List<String> idsMemory = Arrays.asList("AAA", "BBB");
List<String> idsFile = Arrays.asList("CCC", "DDD");
@Mock
FileAccessor fa;
@Before
public void setUp(){
//All the necessary mock initialization and constructors and stuff
infoprovider.setFileAccessor(fa);
}
@Test
public void correctDataAllocationTest(){
List<Information> requiredResult = buildInformation("AAA","BBB","CCC","DDD"); //Prepares Information objects for IDs "AAA","BBB","CCC" and "DDD"
List<Information> infoFromFile = buildInformation("CCC","DDD");
Mockito.when(fa.getInformation(idsFile)).thenReturn(infoFromFile);
infoProvider.getInformation(Arrays.asList("AAA","BBB","CCC","DDD"));
//Asserts and stuff
}
In my case, the method fa.getInformation
is never called with List idsFile
, because the actual list used as a parameter is created new inside the InformationProvider. Both lists do contain the same strings though, "CCC" and "DDD".
How do I make Mockito to match them? All the tutorials I found were dealing only with either objects or used anyList(). Using anyList() actually does the matching, but then loses the information about the IDs. I need to be sure that when called with other IDs, this method returns nothing, something along the lines of (afaik non-existing) "anyListContaining(<Collection>)
".
I am also limited to using Mockito library.