MrkK
MrkK

Reputation: 903

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?

Upvotes: 7

Views: 14183

Answers (1)

Roman C
Roman C

Reputation: 1

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);

Upvotes: 15

Related Questions