I have following two methods which i want to test
public class Orders {
private final LambdaLogger logger;
private final DynamoDBMapper dynamoDBMapper;
public Orders(LambdaLogger logger, AmazonDynamoDB amazonDynamoDB){
this.logger = logger;
this.dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
}
public List<Orders> getOrders(){
logger.log("getting all orders");
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
.withProjectionExpression("OrderId");
logger.log("Expression created");
PaginatedScanList<Orders> scan = dynamoDBMapper.scan(Orders.class, scanExpression);
return scan.stream()
.collect(Collectors.toList());
}
}
Now, I want to do testing using Mockito for this class. There are couple of things that I am confuse (or unable to get working).
First, DynamoDBMapper is being created using amazonDynamoDBClient. So if in my class i Mock AmazonDynamoDB, how the dynamoDBMapper would get created?
How would I test that my function is actually setting projection right?
How would i test on paginatedScanList?
It is violation of Dependency Injection principle that does not allow you to create a unit test.
Orders should not create any objects it should receive them as dependencies through constructor or setter methods. E.g. you could pass DynamoDBScanExpressionFactory and DynamoDBMapper to Orders' constructor. Then you would be able to mock them with mockito.