使用powermockito和mockito不会嘲笑该方法

时间:2019-03-20 10:57:40

标签: amazon-web-services mockito amazon-dynamodb powermockito aws-kms

要测试的方法:

公共地图decryptRecord(字符串测试){

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("SampleTable");
    System.out.println(table);

    final AWSKMS kms = AWSKMSClientBuilder.standard().withRegion(new DefaultAwsRegionProviderChain().getRegion()).build();
    final DirectKmsMaterialProvider cmp = new DirectKmsMaterialProvider(kms, System.getenv("cmkArn"));
    final DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(cmp);
    final EncryptionContext encryptionContext = new EncryptionContext.Builder()
            .withTableName("SampleTable")
            .withHashKeyName("Test")
            .build();

    PrimaryKey TestKey = new PrimaryKey("Test", Test);
    Item item = table.getItem(TestKey);
    System.out.println("Item"+item);

    final Map<String, AttributeValue> record = new HashMap<>();
    record.put("Test", new AttributeValue().withS(item.getString("Test")));
    record.put("status", new AttributeValue().withS(item.getString("status")));
    record.put("connectionType", new AttributeValue().withS(item.getString("connectionType")));
    record.put("type", new AttributeValue().withS(item.getString("type")));
    record.put("*amzn-ddb-map-desc*", new AttributeValue().withB(item.getByteBuffer("*amzn-ddb-map-desc*")));
    record.put("*amzn-ddb-map-sig*", new AttributeValue().withB(item.getByteBuffer("*amzn-ddb-map-sig*")));
    record.put("connectionConfiguration", new AttributeValue().withB(item.getByteBuffer("connectionConfiguration")));

    final EnumSet<EncryptionFlags> signOnly = EnumSet.of(EncryptionFlags.SIGN);
    final EnumSet<EncryptionFlags> encryptAndSign = EnumSet.of(EncryptionFlags.ENCRYPT, EncryptionFlags.SIGN);
    final Map<String, Set<EncryptionFlags>> actions = new HashMap<>();

    for (String attributeName : record.keySet()) {
        switch (attributeName) {
            case "Test": // fall through
                //case sortKeyName: // fall through
                actions.put(attributeName, signOnly);
                break;
            case "connectionConfiguration":
                actions.put(attributeName, encryptAndSign);
                break;
            default:
                // We want to leave everything as is
                break;
        }
    }
    Map<String, AttributeValue> decrypted_record=null;

    try {
        System.out.println("Record before decryption"+ record);
        decrypted_record = encryptor.decryptRecord(record, actions, encryptionContext);
        System.out.println("Decrypted Record: " + decrypted_record);

    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    return decrypted_record;
}

和测试用例:

@RunWith(PowerMockRunner.class) @PrepareForTest({AmazonDynamoDBClientBuilder.class,AWSKMSClientBuilder.class,DynamoDBEncryptor.class}) 公共类DBUtilsImplTest {

@Test
public void testDecryptMethod() throws Exception {

    PowerMockito.mockStatic(AmazonDynamoDBClientBuilder.class);
    AmazonDynamoDBClientBuilder mockamazonDynamoDBClientBuilder =mock(AmazonDynamoDBClientBuilder.class);
    AmazonDynamoDB mockclient=mock(AmazonDynamoDB.class);
    PowerMockito.when(AmazonDynamoDBClientBuilder.standard()).thenReturn(mockamazonDynamoDBClientBuilder);
    PowerMockito.when(mockamazonDynamoDBClientBuilder.build()).thenReturn(mockclient);
    DynamoDB mockdynamoDB=mock(DynamoDB.class);
    PowerMockito.whenNew(DynamoDB.class).withArguments(mockclient).thenReturn(mockdynamoDB);

    Table mocktable=mock(Table.class);

    PowerMockito.when(mockdynamoDB.getTable("SampleTable")).thenReturn(mocktable);
    PowerMockito.mockStatic(AWSKMSClientBuilder.class);

    AWSKMSClientBuilder mockawskmsclientbuilder=mock(AWSKMSClientBuilder.class);
    AWSKMS mockkms= mock(AWSKMS.class);

    String region="us-west-1";
    DefaultAwsRegionProviderChain mockchainprovider=mock(DefaultAwsRegionProviderChain.class);
    PowerMockito.when(mockchainprovider.getRegion()).thenReturn(region);

    PowerMockito.when(AWSKMSClientBuilder.standard()).thenReturn(mockawskmsclientbuilder);
    PowerMockito.when(mockawskmsclientbuilder.withRegion(mockchainprovider.getRegion())).thenReturn(mockawskmsclientbuilder);

    PowerMockito.when(mockawskmsclientbuilder.build()).thenReturn(mockkms);
    DirectKmsMaterialProvider mockcmp=mock(DirectKmsMaterialProvider.class);
    PowerMockito.whenNew(DirectKmsMaterialProvider.class).withArguments(mockkms, System.getenv("cmkArn")).thenReturn(mockcmp);

    PowerMockito.mockStatic(DynamoDBEncryptor.class);

    DynamoDBEncryptor mockencrypter=mock(DynamoDBEncryptor.class);

    PowerMockito.when(DynamoDBEncryptor.getInstance(mockcmp)).thenReturn(mockencrypter);

    PrimaryKey tenantIdKey = new PrimaryKey("Test", 12);
    //PowerMockito.when(mocktable.getItem(tenantIdKey)).thenReturn(new Item());
    Mockito.when(mocktable.getItem(tenantIdKey)).thenReturn(new Item());

    final EncryptionContext encryptionContext = new EncryptionContext.Builder()
            .withTableName("SampleTable")
            .withHashKeyName("Test")
            .build();
    final Map<String, AttributeValue> record = new HashMap<>();
    final Map<String, Set<EncryptionFlags>> actions = new HashMap<>();


    PowerMockito.when(mockencrypter.decryptRecord(record,actions,encryptionContext)).thenReturn(record);
    Map<String, AttributeValue> val=new DynamoDBUtilsImpl().decryptRecord("Test");
    Assert.assertNotNull(val);
}

我在:错误:

java.lang.IllegalArgumentException     在com.amazonaws.services.dynamodbv2.document.GetItemOutcome。(GetItemOutcome.java:33)     在com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.doLoadItem(GetItemImpl.java:78)     在com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItemOutcome(GetItemImpl.java:46)     在com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItem(GetItemImpl.java:88)      at com.amazonaws.services.dynamodbv2.document.Table.getItem(Table.java:597)     在com.nice.tfs.utils.DynamoDBUtilsImpl.decryptRecord(DynamoDBUtilsImpl.java:44)     在com.example.demo.DBUtilsImplTest.testDecryptMethod(DBUtilsImplTest.java:92)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)

寻求原因和解决方案。

0 个答案:

没有答案