I have a test that I am stuck in , it looks like this
public CartItem addBookToCartItem(Book book, User user, int qty) throws AccessDeniedException {
List<CartItem> cartItemList = findByShoppingCart(user.getShoppingCart());
for (CartItem cartItem : cartItemList) {
if (book.getId() == cartItem.getBook().getId()) {
cartItem.setQty(cartItem.getQty() + qty);
cartItem.setSubTotal(new BigDecimal(book.getOurPrice()).multiply(new BigDecimal(qty)));
cartItemRepository.save(cartItem); //it shows there is a problem with this line
return cartItem;
}
}
CartItem cartItem = new CartItem();
cartItem.setShoppingCart(user.getShoppingCart());
cartItem.setBook(book);
cartItem.setQty(qty);
cartItem.setSubTotal(new BigDecimal(book.getOurPrice()).multiply(new BigDecimal(qty)));
cartItem = cartItemRepository.save(cartItem);
BookToCartItem bookToCartItem = new BookToCartItem();
bookToCartItem.setBook(book);
bookToCartItem.setCartItem(cartItem);
bookToCartItemRepository.save(bookToCartItem);
return cartItem;
}
I am posting the whole code cause I don't know where the problem is and how to go about it . My test looks like this
@Test
public void addBookToCartItemTest() throws Exception {
/// entities initialized here
List<CartItem> cartItemList = Arrays.asList(cartItem, cartItem1);
when(cartItemRepository.findByShoppingCart(user.getShoppingCart())).thenReturn(cartItemList);
for (CartItem item : cartItemList) {
if (book1.getId() == item.getBook().getId()) {
item.setQty(item.getQty() + qty);
item.setSubTotal(new BigDecimal(book1.getOurPrice()).multiply(new BigDecimal(qty)));
given(cartItemRepository.save(item)).willReturn(cartItem);
}
}
CartItem newCartItem = new CartItem();
newCartItem.setShoppingCart(user.getShoppingCart());
newCartItem.setBook(book1);
newCartItem.setQty(qty);
newCartItem.setSubTotal(new BigDecimal(book1.getOurPrice()).multiply(new BigDecimal(qty)));
when(cartItemRepository.save(newCartItem)).thenReturn(newCartItem);
BookToCartItem bookToCartItem1 = new BookToCartItem();
bookToCartItem1.setBook(book1);
bookToCartItem1.setCartItem(newCartItem);
when(bookToCartItemRepository.save(bookToCartItem1)).thenReturn(bookToCartItem1);
cartItemService.addBookToCartItem(book1, user, qty);
// Assert.assertNotNull(cartItem1);
// Assert.assertEquals(newCartItem, cartItem1);
Mockito.verify(cartItemRepository).save(newCartItem); //and this line also has a problem
Mockito.verify(bookToCartItemRepository).save(bookToCartItem1);
// Mockito.verify(cartItemRepository).save(item);
error
Argument(s) are different! Wanted:
com.valentine.repository.CartItemRepository#0 bean.save(
com.valentine.domain.CartItem@2f05be7f
);
-> at com.valentine.service.CartItemServiceTest.addBookToCartItemTest(CartItemServiceTest.java:110)
Actual invocation has different arguments:
com.valentine.repository.CartItemRepository#0 bean.save(
com.valentine.domain.CartItem@640f11a1
);
-> at com.valentine.service.CartItemServiceImpl.addBookToCartItem(CartItemServiceImpl.java:48)
Comparison Failure: <Click to see difference>
Argument(s) are different! Wanted:
com.valentine.repository.CartItemRepository#0 bean.save(
com.valentine.domain.CartItem@2f05be7f
);
I think the problem is how tp handle the multiple when methods or is there another way to go about it , Any suggestion would suffice thank you .
Aucun commentaire:
Enregistrer un commentaire