1

I am new in using dynamodb, and I tried to do uniting testing on one of the method.

When I try to mock

DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("id", id, "date", date).withConditionExpression("id = :id")

It returns NPE. How can I mock this with many properties?

Sorry for such a dumb question, but tried whole night for solution.

public void delete(String id) {
    Item item = null;
    Iterator<Item> iterator = getId(id);
    while (iterator.hasNext()) {
        item = iterator.next();
        DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
            //get null pointer on below line
            .withPrimaryKey(new PrimaryKey("id", id, "date", item.getString("date")))
            .withConditionExpression("id = :id")
            .withValueMap(new ValueMap()
            .withString(":id", id))
            .withReturnValues(ReturnValue.ALL_OLD);

        try {
            table.deleteItem(deleteItemSpec);
        } catch (Exception e) {e.getLocalizedMessage(), e);
            throw new dynamoDBException("...")
        }
    }
}

3
  • So, iterator.next() evaluated as null? Commented Aug 25, 2019 at 17:59
  • No the iterator returns as an Mock object from table.query(_) Commented Aug 25, 2019 at 18:30
  • Welcome to SO. Please learn what an MCVE is, then help us understand and reproduce your problem by editing your question accordingly. I like to see a full sample class with package name and imports and a full Spock test, so I can copy, paste, compile and run your test. Even if your test does not work now, show us what you tried and how far you got. From what I understand now this question could be a duplicate of one I answered before about Spock-testing fluent APIs. Commented Aug 26, 2019 at 7:54

0

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.