I am trying to mock a very simple line of code that is used to query the DynamoDB using Java. Here is some sample code for the query -
List<Pojo> result;
try {
if (filters == null) {
this.queryExpression = new DynamoDBQueryExpression<Pojo>()
.withKeyConditionExpression(partitionKeyCondition)
.withExpressionAttributeValues(this.eav);
} else {
setFilterQueryExpression(filters);
}
result = this.dynamoDBMapper.query(Pojo.class, queryExpression);
} catch (final Exception e) {
throw new InternalServerException("Something went wrong with the database query: ", e);
}
The above piece of code works and I am able to retrieve a List of rows that automatically get deserialized into the Pojo.
I am now trying to Mock the this.dynamoDBMapper.query
call as follows -
@Mock
private DynamoDBMapper mapper;
List<Pojo> result = new ArrayList<>();
when(mapper.query(Pojo.class,Mockito.any(DynamoDBQueryExpression.class)).thenReturn(result);
I am unable to do that with error -
Cannot resolve method 'thenReturn(java.util.List<com.amazon.xxx.xxx.Pojo>)'
I also tried another way -
doReturn(result).when(mapper).query(Pojo.class, Mockito.any(DynamoDBQueryExpression.class));
That seems to compile but the test fails with error -
org.mockito.exceptions.misusing.WrongTypeOfReturnValue
I have looked at other sample where the expected output of the query is of type PaginatedQueryList
, I have tried changing to that as well. But I am still not sure why the above throws an error.