Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamodb how do i mock mapper?

I have a section of dynamoDb related code that I want to test using mockito.

The method I want to test contains the following line:

List<NotificationFeedRecord> listResult = mapper.query(NotificationFeedRecord.class, queryExpression);

It's work fine when I test by hand, I submit a query and get the expected results back from dynamodb.

I'm writing unit tests and want to mock mapper.query.

I have:

mapper = mock(DynamoDBMapper.class);
List<NotificationFeedRecord> testList = new ArrayList<>();
when(mapper.query(any(), any())).thenReturn(testList);

Here I get an error

Error:(133, 37) java: no suitable method found for thenReturn(java.util.List<notificationfeed.lib.db.NotificationFeedRecord>)
      (argument mismatch; java.util.List<notificationfeed.lib.db.NotificationFeedRecord> cannot be converted to com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList<java.lang.Object>)

I've tried a range of fixes (e.g. Creating PaginatedQueryList and returning that, changing the query matchers), but all given an error.

The .query method is declared as follows:

public <T> PaginatedQueryList<T> query(Class<T> clazz, DynamoDBQueryExpression<T> queryExpression) {
        return query(clazz, queryExpression, config);
    }

How does one mock mapper.query? Is there something special about it?

like image 810
mark Avatar asked Feb 27 '26 21:02

mark


1 Answers

It was simple really, I had to mock the PaginatedQueryList and then:

when(mapper.query(any(), anyObject())).thenReturn(paginatedQueryList)

;

That worked for me.

These are all the conditions I set-up for our tests:

@Mock private PaginatedQueryList paginatedQueryList;
doReturn(mockQuery).when(utilSpy).getQueryExpression();

when(mockQuery.withFilterExpression(anyString())).thenReturn(mockQuery);
when(mockQuery.withLimit(anyInt())).thenReturn(mockQuery);
when(mockQuery.withExpressionAttributeValues(anyMap())).thenReturn(mockQuery);
when(mockQuery.withIndexName(anyString())).thenReturn(mockQuery);
when(mockQuery.withHashKeyValues(anyString())).thenReturn(mockQuery);
when(mockQuery.withConsistentRead(anyBoolean())).thenReturn(mockQuery); 
when(mockQuery.withRangeKeyCondition(anyString(), anyObject())).thenReturn(mockQuery);
when(mapper.query(any(), anyObject())).thenReturn(paginatedQueryList);
like image 143
mark Avatar answered Mar 02 '26 12:03

mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!