如何编写单元测试以从 java Set 中检索项目
我实现了一个 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 CartItem
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
测试集应该不是问题。
如果您想测试完整的集合,请创建一个相等的集合,并对这两个集合使用 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.基本上有两种方法:
Predicate
类及其Iterables.any
方法。There are basically two ways of doing it:
Predicate
class and itsIterables.any
method.听起来你的购物车正在做很多事情——业务逻辑和数据库的东西。如果您将关注点分成不同的类,您的测试工作将会变得更加容易。与数据库对话的单元测试从来都不是一件好事。
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.