Reputation: 1407
My unit test keeps on sending error messages even though the initial code works and I wrote other unit tests the same way and they work perfectly.
Initial code:
@Override
public void newOrder(OrderVO orderVO) {
OrderProduct orderProduct = new OrderProduct();
Optional<User> userResponse = userRepository.findById(orderVO.getUserId());
User user = userResponse.orElse(new User());
Orders order = new Orders(user);
orderRepository.save(order);
orderProduct.setId(new OrderProductId());
orderProduct.setOrders(order);
List<ProductVO> productVOS = orderVO.getOrderProducts();
for (ProductVO p : productVOS) {
Optional<Product> productResponse = productRepository.findById(p.getId());
Product product = productResponse.orElse(new Product());
product.setAmount(product.getAmount() - p.getAmountOfOrderedProducts());
product.setUser(user);
productRepository.save(product);
orderProduct.setProduct(product);
orderProduct.setAmountOfOrderedProduct(p.getAmountOfOrderedProducts());
orderProductRepository.saveOrder(orderProduct.getProduct().getId(), orderProduct.getOrders().getId(), orderProduct.getAmountOfOrderedProduct());
}
}
test:
@Test
void newOrder() {
User userTest = new User(1);
Optional<User> optUserTest = Optional.of(userTest);
Optional<Product> optProductTest = Optional.of(productTest);
OrderVO orderVO = new OrderVO();
Orders orders = new Orders(userTest);
when(userRepository.findById(anyInt())).thenReturn(optUserTest);
when(productRepository.findById(anyInt())).thenReturn(optProductTest);
orderService.newOrder(orderVO);
verify(productRepository, times(1)).save(productTest);
verify(orderRepository, times(1)).save(orders);
verify(orderProductRepository, times(1)).saveOrder(1,1,53);
}
I keep on getting two errors:
first one is pointing to verify(productRepository, times(1)).save(productTest);
and says that
Wanted but not invoked:
productRepository.save(
com.carlsberg.orderservice.domains.Product@a1db9da0
);
-> at com.carlsberg.orderservice.serviceImpl.OrderServiceImplTest.newOrder(OrderServiceImplTest.java:78)
Actually, there were zero interactions with this mock.
Second one points to verify(orderRepository, times(1)).save(orders);
and the error message is
Argument(s) are different! Wanted:
orderRepository.save(
com.carlsberg.orderservice.domains.Orders@d7c7950f
);
The difference seems to be in d7c7950f
part.
This is my first time trying to do unit testing. Really not sure why am I getting this errors since the same concept of writing unit tests worked just fine on other methods.
Upvotes: 1
Views: 52
Reputation: 12265
The first error is due to the fact that your test never coverages a case where there are ProductVo
s in tested OrderVO
. That makes stubbing that method to be 'wrong' since the method is invoked only in a loop that goes through ProductVO
s.
Try to add some ProductVO
s if it helps for this. Maybe you still might want to test with 0 1 and many OrderVOs
in 0 case just remove the stubbing as it is not needed
The second one is a bit more complicated, in touyr service you have:
Orders order = new Orders(user);
orderRepository.save(order);
on the other hand in your test you have:
Orders orders = new Orders(userTest);
the is no path that saves the orders you created in your test , it is alwasy created in the service and the hassh is thus also different. I guess this error goes away when you change verify to, say:
verify(orderRepository, times(1)).save(any());
but the test is bad and you should fix it to use the orders you create in test.
Upvotes: 1