Bacon
Bacon

Reputation: 1279

Mockito ambiguous method call

I'm trying to stub out a method call, e.g.

when(amazonDynamoDBClient.batchWriteItem(anyObject())).thenReturn(batchWriteItemResultMock);

I get this error

Error:(198, 34) java: reference to batchWriteItem is ambiguous
  both method batchWriteItem(com.amazonaws.services.dynamodbv2.model.BatchWriteItemRequest) in com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient and method batchWriteItem(java.util.Map<java.lang.String,java.util.List<com.amazonaws.services.dynamodbv2.model.WriteRequest>>) in com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient match

I can't see how this is ambiguous - the method signatures are different, i.e.

public BatchWriteItemResult batchWriteItem(BatchWriteItemRequest request) {

and

public BatchWriteItemResult batchWriteItem(Map<String, List<WriteRequest>> requestItems) {

What am I doing wrong here?

Upvotes: 18

Views: 29052

Answers (2)

DinDin
DinDin

Reputation: 39

For me, using isA() fixed the issue.

Docs : IsA

Upvotes: 2

QBrute
QBrute

Reputation: 4534

You have two methods with the same name and return type, each with one parameter. So anyObject() matches both of them. That's why you get the batchWriteItem is ambiguous message.

You could use Mockito.any(Class<T> type) and Mockito.anyMapOf(Class<K> keyClazz, Class<V> valueClazz) to distinguish between them.

Docs for reference: any, anyMapOf

Upvotes: 43

Related Questions