我有以下代码,我试图模仿:
public void getOrders(Response response){
logger.log("Getting all orders");
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
.withProjectionExpression("OrderId");
PaginatedScanList<Orders> orders = dynamoDBMapper.scan(Orders.class, scanExpression);
response.setData(orders..stream()
.collect(Collectors.toList()));
}
我试图模仿的方式是:
Mockito.when(mockDynamoDBMapper.scan(Orders.class,
Mockito.any())).thenReturn(mockPaginatedList);
我得到以下异常:
如果匹配器与原始值组合,则可能会发生此异常: //不正确: someMethod(anyObject(),&#34; raw String&#34;);使用匹配器时,所有参数都必须由匹配器提供。例如: //正确: someMethod(anyObject(),eq(&#34; matcher by matcher&#34;)); 有关更多信息,请参阅Matchers类的javadoc。
我应该如何使用任何dbmapper.scan
对象模拟DynamoDBScanExpression
方法?
答案 0 :(得分:2)
错误对您的问题有明确的答案。你想要的是:
Mockito.when(mockDynamoDBMapper.scan(eq(Orders.class),
Mockito.any())).thenReturn(mockPaginatedList);
必须为匹配器提供匹配器 - eq(Orders.class),而不是原始值 - Orders.class
答案 1 :(得分:0)
一般来说,我所做的是首先模拟 PaginatedScanList,它是 mapper.scan() 给我们的响应类型,然后我将该响应模拟分配给它在扫描 () 时为映射器创建的模拟被称为
// here I define the values that will be assigned to the filter
Map <String, AttributeValue> expressionAttributeValues = new HashMap <> ();
expressionAttributeValues.put (": attributeValue", new AttributeValue (). withS ("value"));
// the definition of the epxpression for the scan
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression ();
scanExpression.setLimit (1); // amount of data to return
scanExpression.setFilterExpression (("email =: attributeValue")); // the attribute to filter by
scanExpression.setProjectionExpression ("id"); // the data to return from the row or json
scanExpression.setExpressionAttributeValues (expressionAttributeValues); // place the value by which to filter
DynamoDBMapper mapper = mock (DynamoDBMapper.class); // The mock was created for the mapper, which is the one with different methods such as query, scan, etc.
PaginatedScanList scanResultPage = mock (PaginatedScanList.class); // definition of the mock for the scan result
when (scanResultPage.size ()). then (invocation -> 3); // what to return when the PaginatedScanList is called
when (mapper.scan (Person.class, scanExpression)). then (invocation -> scanResultPage); // here returns our mocked PaginatedScanList for the scan function