我有以下我要模拟的代码:
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((," RAW String"(;使用匹配项时,所有参数都必须由匹配者提供。例如: //正确的: someMethod(AnyObject((,eq(" a string by Matcher"((; 有关更多信息,请参见Matchers类的Javadoc。
如何使用任何DynamoDBScanExpression
对象模拟dbmapper.scan
方法?
错误对您的问题有明确的答案。您想要的是:
Mockito.when(mockDynamoDBMapper.scan(eq(Orders.class),
Mockito.any())).thenReturn(mockPaginatedList);
必须向匹配器提供匹配器-EQ(orders.class(,而不是原始值-Class.Class
通常,我要做的是首先模拟PaginedScanlist,这是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