Mockito ambiguous method call

2022-09-03 09:35:53

我正在尝试存根方法调用,例如

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

我收到此错误

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

我看不出这是如何模棱两可的 - 方法签名是不同的,即

public BatchWriteItemResult batchWriteItem(BatchWriteItemRequest request) {

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

我在这里做错了什么?


答案 1

您有两个具有相同名称和返回类型的方法,每个方法都有一个参数。所以匹配他们俩。这就是您收到消息的原因。anyObject()batchWriteItem is ambiguous

您可以使用 和 来区分它们。Mockito.any(Class<T> type)Mockito.anyMapOf(Class<K> keyClazz, Class<V> valueClazz)

供参考的文档:任何任何地图


答案 2

对我来说,使用解决了这个问题。isA()

文档 : IsA


推荐