2

I wrote the dynamoDB code which stores list of items.

mapper.batchSave(trafficSensorReadings)

This will return.

List<FailedBatch>

I want to mock the mapper.batchSave and then return one failed job. How can I achieve it? I am using mockito and Junit.

I wrote something like this. But not useful.

        when(dynamoDBMapper.batchSave(eq(List.class))).thenReturn(mock(List.class));

2 Answers 2

2

A complete example follows

@Test
public void test() {
    FailedBatch failedBatch = mock(FailedBatch.class);
    List<FailedBatch> failedBatchList = new ArrayList<>();
    failedBatchList.add(failedBatch);
    DynamoDBMapper dynamoDBMapperMock = mock(DynamoDBMapper.class);
    when(dynamoDBMapperMock.batchSave(any(List.class))).thenReturn(failedBatchList);

    tested.testedMethodCall();

    verify(dynamoDBMapperMock).batchSave(any(List.class));
}
Sign up to request clarification or add additional context in comments.

Comments

1

First, I think you might want to use Mockito.any() instead of Mockito.eq().

Second, I don't see why you would want to mock the list. You can just create one and return it

// GIVEN
FailedBatch batch1 = /**/;
FailedBatch batch2 = /**/;
List<FailedBatch> failedBatchList = Lists.newArrayList(batch1, batch2);

// WHEN
when(dynamoDBMapper.batchSave(any(List.class))).thenReturn(failedBatchList);

Object someResult = yourFunctionTestCall();

// THEN
verify(someResult)...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.