How to Unit Test DynamoDB Enhanced Client Transaction Request

Viewed 9

So I am having a problem trying to setup unit test for my application because it seems like the Transaction Request does some parameter/table processing behind the scenes when the Builder builds the TransactionRequest. Snippet of code below for reference.

public CompletableFuture<Void> createNewUser(User user) {

     TransactPutItemEnhancedRequest<Profile> profileRequest = TransactPutItemEnhancedRequest.builder(Profile.class)
            .conditionExpression(Expression.builder()
                    .expression("attribute_not_exists(profileId)")
                    .build())
            .item(profile)
            .build();

    TransactPutItemEnhancedRequest<User> userRequest = TransactPutItemEnhancedRequest.builder(User.class)
            .conditionExpression(Expression.builder()
                    .expression("attribute_not_exists(userId)")
                    .build())
            .item(user)
            .build();

    TransactWriteItemsEnhancedRequest request = TransactWriteItemsEnhancedRequest.builder()
            .addPutItem(usersTableViaUser, userRequest)                
            .addPutItem(profilesTable, profileRequest)
            .build();  // CAUSES NPE

    etc...
}

Tables Injected into the class

DynamoDbAsyncTable<Profile> profilesTable,
DynamoDbAsyncTable<User> usersTableViaUser,

Problem

The problem is, it seems like on the build() of the Transaction, the Builder class is looking for basically a bunch of the fields for which the Mocked DynamoDbAsyncTable does not provide. With that being said, can anyone see a way on how to get around this without manually having the mock return these values? Really not a big fan of how this was designed, I wish this would have been pushed off to when the actual call to dynamodb was made..

//TransactWriteItemsEnhancedRequest Class
private <T> TransactWriteItem generateTransactWriteItem(MappedTableResource<T> mappedTableResource, TransactableWriteOperation<T> generator) {
      return generator.generateTransactWriteItem(mappedTableResource.tableSchema(), DefaultOperationContext.create(mappedTableResource.tableName()), mappedTableResource.mapperExtension());
}
0 Answers
Related