3

Below is the code which I am trying to Mock,

this.dynamoDb.getTable(PropertyUtil.get(Constants.SOME_TABLE_NAME))
        .putItem(
                new PutItemSpec()
                        .withItem(new Item().withString("ID", pId).withString("eId", pEId)
                                .withString("activeInd", pActiveInd)));

What I have tried is below,

mockStatic(AmazonDynamoDBClientBuilder.class);
    when(AmazonDynamoDBClientBuilder.defaultClient()).thenReturn(Mockito.mock(AmazonDynamoDB.class));
    PowerMockito.mockStatic(PropertyUtil.class);
    when(PropertyUtil.get(Constants.Some__ID_MAPPING_TABLE_NAME)).thenReturn("SOME_TABLE_NAME");
    Table vTable = mock(Table.class);
    PutItemSpec vPutItemSpec = mock(PutItemSpec.class);
    PutItemResult vPutItemResult = new PutItemResult();
    PowerMockito.whenNew(PutItemSpec.class).withAnyArguments().thenReturn(vPutItemSpec);
    PowerMockito.when(vTable.putItem(vPutItemSpec)).thenReturn(vPutItemOutcome);

And the error that I get due to this is below,

java.lang.IllegalArgumentException: null
    at com.amazonaws.services.dynamodbv2.document.PutItemOutcome.<init>(PutItemOutcome.java:33) ~[aws-java-sdk-dynamodb-1.11.400.jar:?]
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.doPutItem(PutItemImpl.java:86) ~[aws-java-sdk-dynamodb-1.11.400.jar:?]
    at com.amazonaws.services.dynamodbv2.document.internal.PutItemImpl.putItem(PutItemImpl.java:63) ~[aws-java-sdk-dynamodb-1.11.400.jar:?]
    at com.amazonaws.services.dynamodbv2.document.Table.putItem(Table.java:168) ~[aws-java-sdk-dynamodb-1.11.400.jar:?]

Please suggest, what I need to fix, I am new to Mock and JUnit. Thanks in Advance.

ViS
  • 1,357
  • 1
  • 17
  • 36
  • Try with Map resourceResult = new HashMap(); PowerMockito.whenNew(PutItemSpec.class).withItem(resourceResult).thenReturn(vPutItemSpec); – IMParasharG Mar 11 '19 at 16:38

3 Answers3

1

Using: - Mockito - Junit

In my case I have to declare in my Junit a parameter type Table(import com.amazonaws.services.dynamodbv2.document.Table) with anotation @Mock

Then en my @Test I have to use Mockito.when() with a mock return, here, my example code:

public class ClassName {

  @Mock private Table tableMock;

  @Test
  public void shouldMockTableInfoForDynamoBDTable() {

    Mockito.when(tableMock.getItem((GetItemSpec) any()))
        .thenReturn(
            Item.fromJSON(
                "json: example"));
    subject.myMethodUnderTest();
  }
}
EdwinCab
  • 361
  • 1
  • 3
  • 17
0

You could make your life a lot easier using DynamoDBMapper. It's an object mapping SDK from AWS. It means you create the Plain Old Java Objects you want to handle in your code, and simply add annotations to the classes which then maps the objects to DynamoDB.

The benefits are much cleaner code, as well as making your objects trivial to mock.

In your case, this code:

this.dynamoDb.getTable(PropertyUtil.get(Constants.SOME_TABLE_NAME))
        .putItem(
                new PutItemSpec()
                        .withItem(new Item().withString("ID", pId).withString("eId", pEId)
                                .withString("activeInd", pActiveInd)));

Would become:

SOME_TABLE_NAME thing = new SOME_TABLE_NAME().id(pId).eId(pEid).activeInd(pActiveInd);
Mapper.save(thing);

Or similar. As you can see, much cleaner.

F_SO_K
  • 13,640
  • 5
  • 54
  • 83
  • But even this line will be needed to be mocked. My query is about need to mock some dependency. – ViS Mar 12 '19 at 18:01
0
this.dynamoDb
  .getTable(PropertyUtil.get(Constants.SOME_TABLE_NAME))
        .putItem(new PutItemSpec()
              .withItem(new Item().withString("ID", pId).withString("eId", pEId)
              .withString("activeInd", pActiveInd)));

Why you don't do this.

AmazonDynamoDB dynamoDb = Mockito.mock(AmazonDynamoDB.class);
Table mockTable = Mockito.mock(Table.class)
PutItemOutcome itemOutcome = Mockito.mock(PutItemOutcome.class)    
Mockito.when(mockTable.putItem(Mockito.any(PutItemSpec.class)).thenReturn(itemOutcome);
Mockito.when(dynamoDb.getTable(Mockito.any(Property.class)).thenReturn(mockTable);

I think you don't need to mock the PutItemSpec by hand. the other option is just use

Mockito.when(mockTable.putItem(Mockito.any(PutItemSpec.class)). thenCallRealMethod();
Koitoer
  • 18,778
  • 7
  • 63
  • 86