Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mockito's argThat matcher causes "method ambiguous for the type" error

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?

like image 219
MrkK Avatar asked Mar 01 '16 14:03

MrkK


1 Answers

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);
like image 96
Roman C Avatar answered Oct 19 '22 10:10

Roman C