0

I have to write unit test case using mockito for the below code

QuerySpec querySpec = new QuerySpec()
                .withKeyConditionExpression(EXPRESSION)
                .withValueMap(
                        new ValueMap()
                                .withString(ID_PLACEHOLDER, workItemId));
ItemCollection<QueryOutcome> items = index.query(querySpec);
List<Record> recordsList = new ArrayList<>();
for(Item item: items) {
     recordsList.add(gson.fromJson(item.toJSON(), Record.class));
}
return recordList;

I can see similar answer using easy mock How to mock DynamoDB's ItemCollection<QueryResult> using EasyMock?. How to do write unit test case for the function above using mockito?

Nitika
  • 105
  • 3
  • 18

1 Answers1

-1
ItemCollection<QueryOutcome> items = mock(ItemCollection.class);
when(index.query(any(QuerySpec.class)).thenReturn(items);

The above code can do what you want, but I don't see why you would want to mock ItemCollection in this case. Rather return a real ItemCollection with values that you've manually inserted as below>

ItemCollection<QueryOutcome> items = new ItemCollection<>();
items.add(...)
when(index.query(any(QuerySpec.class)).thenReturn(items);
Ratshiḓaho Wayne
  • 743
  • 1
  • 5
  • 23
  • 1
    ItemCollection is abstract and cannot be initialized when returning a real itemcollection with values. – Nitika Sep 20 '21 at 05:41