Issue
I am trying to mock a method which takes a map and couple other arguments as parameters. My goal is to match the map entries. Originally I defined my mock as:
when(discoveryJobCatalogResourceAccessor.findResource(argThat(allOf(hasEntry("start", "testStart"), hasEntry("level", "testLevel"))), any(Integer.class),
any(Integer.class), any(String.class), any(String.class))).thenReturn(searchResponse);
This causes the following error:
The method findResource(Map<String,String>, Integer, Integer, String, String) is ambiguous for the type DiscoveryJobCatalogResourceAccessor
When I replace argThat()
with any(HashMap.class)
like this:
when(discoveryJobCatalogResourceAccessor.findResource(any(HashMap.class), any(Integer.class), any(Integer.class), any(String.class), any(String.class))).thenReturn(searchResponse);
the error is gone, but in this case I cannot match the map values. It seems that Mockito's argThat()
causes the ambiguity. I am wondering if there is a way to use the argThat()
without causing an error?
Solution
You can cast to the required type
when(discoveryJobCatalogResourceAccessor.findResource(HashMap<String,String>)argThat(allOf(hasEntry("start", "testStart"), hasEntry("level", "testLevel"))), any(Integer.class),
any(Integer.class), any(String.class), any(String.class))).thenReturn(searchResponse);
Answered By - Roman C
Answer Checked By - David Goodson (JavaFixing Volunteer)