DynamoDB put Item没有使用MockiTo和PowerMock进行模拟

问题描述 投票:3回答:2

下面是我试图模拟的代码,

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

我试过的是下面的,

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);

我得到的错误在于,

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:?]

请提出建议,我需要修复,我是Mock和JUnit的新手。提前致谢。

java mockito amazon-dynamodb junit4 powermockito
2个回答
0
投票

使用DynamoDBMapper可以让您的生活更轻松。它是从AWS映射SDK的对象。这意味着您要在代码中创建要处理的Plain Old Java对象,只需向类添加注释,然后将对象映射到DynamoDB。

好处是更清晰的代码,以及使您的对象琐碎模拟。

在您的情况下,此代码:

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

会成为:

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

或类似的。如你所见,更清洁。


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)));

为什么你不这样做。

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);

我认为你不需要手工模拟PutItemSpec。另一种选择就是使用

Mockito.when(mockTable.putItem(Mockito.any(PutItemSpec.class)). thenCallRealMethod();
© www.soinside.com 2019 - 2024. All rights reserved.