当我不知道随机id是数据库项的primaryKey时,如何测试方法

时间:2017-09-28 15:40:23

标签: java unit-testing amazon-web-services junit amazon-dynamodb

你能告诉我如何测试编辑方法,我必须匹配两个产品,但我不能这样做因为我不知道随机生成的primaryKey。

这是方法代码:

public Product editProduct(PrimaryKey primaryKey, Product content) {

    Map<String, Object> map = new HashMap<>();
    map.put("name", content.getName());
    map.put("calories", content.getCalories());
    map.put("fat", content.getFat());
    map.put("carbo", content.getCarbo());
    map.put("protein", content.getProtein());
    map.put("productKinds", content.getProductKinds());
    map.put("author", content.getAuthor());
    map.put("media", content.getMedia());
    map.put("approved", content.getApproved());

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withValueMap(map);

    UpdateItemOutcome itemOutcome = databaseController.getTable(PRODUCT_TABLE).updateItem(updateItemSpec);


    Product product = new Product();
    product.setName(itemOutcome.getItem().get("name").toString());
    product.setCalories(itemOutcome.getItem().getInt("calories"));
    product.setFat(itemOutcome.getItem().getDouble("fat"));
    product.setCarbo(itemOutcome.getItem().getDouble("carbo"));
    product.setProtein(itemOutcome.getItem().getDouble("protein"));
    product.setProductKinds(itemOutcome.getItem().getList("productKinds"));

    ObjectMapper objectMapper = new ObjectMapper();
    try {
        Author productAuthor = objectMapper.readValue(itemOutcome.getItem().getString("author"), Author.class);
        product.setAuthor(productAuthor);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        Media productMedia = objectMapper.readValue(itemOutcome.getItem().getString("media"), Media.class);
        product.setMedia(productMedia);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return product;
}

我想我做得很好,但如果你看错了,请告诉我。我是先生,我以前从未使用过AWS。

这是测试代码:

@Test
public void editProduct() throws Exception {

    KitchenService instance = new KitchenService(databaseControllerMock, loggerMock);

    //TODO prepare expected product fields
    Product expectedProduct = new Product();
    Map<String, Object> map = new HashMap<>();
    map.put("name", expectedProduct.getName());
    map.put("calories", expectedProduct.getCalories());
    map.put("fat", expectedProduct.getFat());
    map.put("carbo", expectedProduct.getCarbo());
    map.put("protein", expectedProduct.getProtein());
    map.put("productKinds", expectedProduct.getProductKinds());
    map.put("author", expectedProduct.getAuthor());
    map.put("media", expectedProduct.getMedia());
    map.put("approved", expectedProduct.getApproved());



    //TODO prepare argument product
    Product productToSave = new Product();
    productToSave.setName("kaszanka");
    productToSave.setCalories(1000);
    productToSave.setFat(40.00);
    productToSave.setCarbo(20.00);
    productToSave.setProtein(40.00);
    productToSave.setProductKinds(Collections.singletonList(ProductKind.MEAT));
    productToSave.setApproved(false);
    Author author = new Author();
    author.setId("testID");
    author.setName("Endrju Golota");
    productToSave.setAuthor(author);
    Media media = new Media();
    media.setMediaType(MediaType.IMAGE);
    media.setName("dupajasia");
    media.setUrl("http://blabla.pl");
    productToSave.setMedia(media);



    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withValueMap(map);

    UpdateItemOutcome itemOutcome = databaseControllerMock.getTable("product").updateItem(updateItemSpec);


    when(databaseControllerMock.update(any(Item.class))).thenReturn(itemOutcome);

    Product actualProduct = instance.editProduct(productToSave);

    assertEquals(expectedProduct, actualProduct);

}

我在测试方面遇到了很多问题但是我仍然在学习如何正确地做到这一点,如果你在我的代码中看到一些荒谬的事情,那就不要愚蠢了......

1 个答案:

答案 0 :(得分:0)

你不应该断言不可预测的事情。而不是使用assertEquals来比较整个对象,您只需使用许多assertEquals测试每个可预测的字段。

您的editProduct方法现在的方式不是单元可测试的。我会将editProduct方法分解为较小的方法。类似的东西:

public Product editProduct(PrimaryKey primaryKey, Product content) {

    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withValueMap(createMap(content));

    UpdateItemOutcome itemOutcome = databaseController.getTable(PRODUCT_TABLE).updateItem(updateItemSpec);

    return  toProduct(itemOutcome);

}

public Map<String, Object> createMap(Product content) {

    Map<String, Object> result = new HashMap<>();

    result.put("name", content.getName());
    result.put("calories", content.getCalories());
    result.put("fat", content.getFat());
    result.put("carbo", content.getCarbo());
    result.put("protein", content.getProtein());
    result.put("productKinds", content.getProductKinds());
    result.put("author", content.getAuthor());
    result.put("media", content.getMedia());
    result.put("approved", content.getApproved());

    return result;
}

public Product toProduct(UpdateItemOutcome outcome) {

    Product product = new Product();
    product.setName(itemOutcome.getItem().get("name").toString());
    product.setCalories(itemOutcome.getItem().getInt("calories"));
    product.setFat(itemOutcome.getItem().getDouble("fat"));
    product.setCarbo(itemOutcome.getItem().getDouble("carbo"));
    product.setProtein(itemOutcome.getItem().getDouble("protein"));
    product.setProductKinds(itemOutcome.getItem().getList("productKinds"));

    ObjectMapper objectMapper = new ObjectMapper();
    try {
        Author productAuthor = objectMapper.readValue(itemOutcome.getItem().getString("author"), Author.class);
        product.setAuthor(productAuthor);
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        Media productMedia = objectMapper.readValue(itemOutcome.getItem().getString("media"), Media.class);
        product.setMedia(productMedia);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return product;

}

现在你可以将较小的方法单元测试到Product和createMap,看看它们是否像你期望的那样。

正如你在评论中所说,你需要模拟数据库,因为这是一个单元测试。