如何编写单元测试以从 java Set 中检索项目

发布于 2024-12-03 06:17:27 字数 1236 浏览 1 评论 0原文

我实现了一个 ShoppingCart,其中 CartItem 被放入 Set 中以避免重复项目。我想测试将商品放入购物车并保留在数据库中,然后从购物车中检索商品并检查它们是否相同。

我尝试按如下方式执行此操作,但看起来很丑。它仅适用于单个项目。如果使用List来存储CartItems,问题就可以解决。

class ShoppingCart{
   private Set<CartItem> cartItems;
   private User customer;
   public ShoppingCart(User customer) {
    super();
    this.cartItems = new TreeSet<CartItem>();
    this.customer = customer;
   }
   ...
}

class CartItem{
   private Product pdt;
   private int quantity;
   ...
}

单元测试

User bob = getUserByName("bob");
CartItem citem1 = new CartItem(product1,3);
ShoppingCart cart = new ShoppingCart(bob);
cart.addItem(citem1);
cart.saveToDB();
ShoppingCart bobCart = getCartFromDbUsingCustomer(bob);
assertNotNull(bobCart);
Set<CartItem> bobCartItems = bobCart.getCartItems();
assertEquals(1,bobCartItems.size());
CartItem[] citems = bobCartItems.toArray(new CartItem[0]);
CartItem bobCartItem = citems[0];
assertEquals("978",bobCartItem.getProduct().getIsbn());
assertEquals(3,bobCartItem.getQuantity());

这仅在将单个购物车项目添加到购物车时才起作用。如果添加两个或多个购物车项目,则仅当使用某种比较器时我才能取消这些项目。我有一种感觉,这不是正确的做法。

有什么建议吗?

I have implemented a ShoppingCart where the CartItems are put in a Set to avoid duplicate items. I want to test putting items in cart and persisting in db, and then retrieving the items from cart and checking if they were the same .

I tried to do this as below, but it looks ugly. It will work only for a single item. The problem would have been solved if a List was used to store CartItems.

class ShoppingCart{
   private Set<CartItem> cartItems;
   private User customer;
   public ShoppingCart(User customer) {
    super();
    this.cartItems = new TreeSet<CartItem>();
    this.customer = customer;
   }
   ...
}

class CartItem{
   private Product pdt;
   private int quantity;
   ...
}

unit test

User bob = getUserByName("bob");
CartItem citem1 = new CartItem(product1,3);
ShoppingCart cart = new ShoppingCart(bob);
cart.addItem(citem1);
cart.saveToDB();
ShoppingCart bobCart = getCartFromDbUsingCustomer(bob);
assertNotNull(bobCart);
Set<CartItem> bobCartItems = bobCart.getCartItems();
assertEquals(1,bobCartItems.size());
CartItem[] citems = bobCartItems.toArray(new CartItem[0]);
CartItem bobCartItem = citems[0];
assertEquals("978",bobCartItem.getProduct().getIsbn());
assertEquals(3,bobCartItem.getQuantity());

This would only work if a single cart item is added to the cart.If two or more cartItems are added, I will be able to retireve the items only if some kind of comparator is used. I have a feeling that, this is not quite the right way to do this.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假情假意假温柔 2024-12-10 06:17:27

测试集应该不是问题。

如果您想测试完整的集合,请创建一个相等的集合,并对这两个集合使用 equals() 方法。确保集合中的元素也实现 equals()。

如果您不想测试完整的集合,可以通过编程方式访问它,或使用hamcrest 匹配器,例如hasItem()/hasItems()/anyOf()/allOf()。无论如何,我强烈推荐 hamcrest 进行测试,因为它的测试更简洁,测试可读性更好,测试失败的可读性更好。

Testing sets should not be a problem.

If you want to test your complete set, create an equal one and use the equals() method on the two sets. Be sure that the elements in the set also implement equals().

If you do not want to test your complete set, you can either access it programmatically, or use hamcrest matchers, e.g. hasItem()/hasItems()/anyOf()/allOf(). I highly recommend hamcrest for testing anyways, because of more concise tests, better readable tests, and better readable test failures.

肤浅与狂妄 2024-12-10 06:17:27

基本上有两种方法:

  • 使用确定性顺序将集合转换为列表,并测试列表中的所有项目是否都是预期项目。您确实需要一个比较器。
  • 使用一些谓词并测试集合中是否有任何项目满足谓词。 Guava 在这里可能有用,它的 Predicate 类及其 Iterables.any 方法。

There are basically two ways of doing it:

  • transform the set into a list using a deterministic order, and test if all items in the list are the expected items. You will indeed need a comparator.
  • use some predicate and test if any item in the set satisfies the predicate. Guava might be useful here with its Predicate class and its Iterables.any method.
千纸鹤带着心事 2024-12-10 06:17:27

听起来你的购物车正在做很多事情——业务逻辑和数据库的东西。如果您将关注点分成不同的类,您的测试工作将会变得更加容易。与数据库对话的单元测试从来都不是一件好事。

Sounds like your shoppingcart is doing an awful lot -- businesslogic AND database stuff. If you separated concerns into different classes, your testing job would become much easier. A unittest talking to a database is never a good thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文