mercredi 31 janvier 2018

java lang AssertionError For Service Test

I'm writing a Mockito test for a service method which looks like this but I am an error that says what I expect to save is not what I actually saved and the repositories are mocked by @MockBean annotations

 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);
                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 have written a test which I think is ideal and should pass but I get a null value in my assertion Test:

 @RunWith(SpringRunner.class)
    @SpringBootTest(classes = CartItemServiceImpl.class)
    public class CartItemServiceTest {

        @Test
            public void addBookToCartItemTest() throws Exception {


                Book bookForCart = new Book();
                        bookForCart.setOurPrice(12);

                ShoppingCart myCart = new ShoppingCart();
                myCart.setId(1L);
                 //cart do somethin
                User userForCart = new User();
                userForCart.setId(2L);

                int qtyForBook = 5;

                CartItem cartItem = new CartItem();

                cartItem.setShoppingCart(userForCart.getShoppingCart());


                when(cartItemRepository.save(cartItem)).thenReturn(cartItemme);

                BookToCartItem bookToCartItem = new BookToCartItem();
                bookForCart.setId(1L);

                bookToCartItem.setBook(bookForCart);

                bookToCartItem.setCartItem(cartItemme);

                BookToCartItem bookToCartItem1 = new BookToCartItem();

                when(bookToCartItemRepository.save(bookToCartItem)).thenReturn(bookToCartItem1);

                CartItem cartItem1  =  cartItemService.addBookToCartItem(bookForCart, userForCart, qtyForBook);

                 Assert.assertNotNull(cartItem1);
                Mockito.verify(cartItemRepository).save(cartItem1);
                Mockito.verify(bookToCartItemRepository).save(bookToCartItem);

        }
    My log

        Argument(s) are different! Wanted:
        com.valentine.repository.CartItemRepository#0 bean.save(
            com.valentine.domain.CartItem@78365cfa
        );
        -> at com.valentine.service.CartItemServiceTest.addBookToCartItemTest(CartItemServiceTest.java:112)
        Actual invocation has different arguments:
        com.valentine.repository.CartItemRepository#0 bean.save(
            com.valentine.domain.CartItem@64a8c844
        );
        -> at com.valentine.service.CartItemServiceImpl.addBookToCartItem(CartItemServiceImpl.java:59)

Now it says cart item saved is not what I get in return, yes but why and what can to do to correct it?

Aucun commentaire:

Enregistrer un commentaire