简体   繁体   English

DynamoDB put Item 不会使用 MockiTo 和 PowerMock 进行模拟

[英]DynamoDB put Item is not getting mocked using MockiTo & PowerMock

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.请建议,我需要修复什么,我是 Mock 和 JUnit 的新手。 Thanks in Advance.提前致谢。

Using: - Mockito - Junit使用: - 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在我的情况下,我必须在我的Junit声明一个带有注释的参数类型Table (导入 com.amazonaws.services.dynamodbv2.document.Table) @Mock

Then en my @Test I have to use Mockito.when() with a mock return, here, my example code:然后我的@Test我必须使用Mockito.when()和模拟返回,这里是我的示例代码:

public class ClassName {

  @Mock private Table tableMock;

  @Test
  public void shouldMockTableInfoForDynamoBDTable() {

    Mockito.when(tableMock.getItem((GetItemSpec) any()))
        .thenReturn(
            Item.fromJSON(
                "json: example"));
    subject.myMethodUnderTest();
  }
}

You could make your life a lot easier using DynamoDBMapper .使用DynamoDBMapper可以让您的生活更轻松。 It's an object mapping SDK from AWS.它是来自 AWS 的对象映射 SDK。 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.这意味着您创建了要在代码中处理的普通旧 Java 对象,只需向类添加注释,然后将对象映射到 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.如您所见,干净多了。

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.我认为您不需要手动模拟 PutItemSpec。 the other option is just use另一种选择是使用

Mockito.when(mockTable.putItem(Mockito.any(PutItemSpec.class)). thenCallRealMethod();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM