Skip to content
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

Merged
merged 9 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions cart/src/test/java/com/yas/cart/service/CartServiceTest.java
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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilize @Injectmocks and @mock annotation instead of using mock() explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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() {
Copy link
Contributor

@minhtridn2001 minhtridn2001 Sep 24, 2024

Choose a reason for hiding this comment

The 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);
}
}
4 changes: 2 additions & 2 deletions common-library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<parent>
<groupId>com.yas</groupId>
<artifactId>yas</artifactId>
<version>1.0-SNAPSHOT</version>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>common-library</artifactId>
<version>1.0-SNAPSHOT</version>
<version>${revision}</version>
<packaging>jar</packaging>

<name>Common Library</name>
Expand Down
Loading
Loading