-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#829 - Config common library and add test for product, cart,... for checking #1057
Changes from 8 commits
c57d787
5d2b523
ed1404f
db6bccf
882be1b
9b93352
09cd3d6
83a67ad
1904513
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.yas.cart.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.anyList; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.yas.cart.model.Cart; | ||
import com.yas.cart.repository.CartItemRepository; | ||
import com.yas.cart.repository.CartRepository; | ||
import com.yas.cart.viewmodel.CartItemVm; | ||
import com.yas.commonlibrary.exception.BadRequestException; | ||
import com.yas.commonlibrary.exception.NotFoundException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CartServiceTest { | ||
|
||
private CartService cartService; | ||
private CartRepository cartRepository; | ||
private CartItemRepository cartItemRepository; | ||
private ProductService productService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
cartRepository = mock(CartRepository.class); | ||
cartItemRepository = mock(CartItemRepository.class); | ||
productService = mock(ProductService.class); | ||
cartService = new CartService(cartRepository, cartItemRepository, productService); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utilize @Injectmocks and @mock annotation instead of using mock() explicitly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve updated the code to utilize @Injectmocks and @mock annotations. Please take a review, thank you. |
||
} | ||
|
||
@Test | ||
void testAddToCart_ProductNotFound() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name should describe the condition and expected outcome as well. |
||
List<CartItemVm> cartItemVms = Collections.singletonList(new CartItemVm(1L, 2, null)); | ||
when(productService.getProducts(anyList())).thenReturn(Collections.emptyList()); | ||
|
||
assertThrows(NotFoundException.class, () -> cartService.addToCart(cartItemVms)); | ||
} | ||
|
||
@Test | ||
void testUpdateCartItems_ItemNotFound() { | ||
String customerId = "customerId"; | ||
CartItemVm cartItemVm = new CartItemVm(1L, 2, null); | ||
when(cartRepository.findByCustomerIdAndOrderIdIsNull(customerId)) | ||
.thenReturn(Collections.singletonList(new Cart())); | ||
|
||
assertThrows(BadRequestException.class, () -> cartService.updateCartItems(cartItemVm, customerId)); | ||
} | ||
|
||
@Test | ||
void testCountNumberItemInCart_EmptyCart() { | ||
String customerId = "customerId"; | ||
when(cartRepository.findByCustomerIdAndOrderIdIsNull(customerId)).thenReturn(Collections.emptyList()); | ||
|
||
Long result = cartService.countNumberItemInCart(customerId); | ||
|
||
assertEquals(0L, result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why there are no happy cases in this test class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cart test class is designed solely to test the functionality of the common library. Since the cart module already has high code coverage, I’ve only added a few cases to trigger the pipeline