From f04ec7a0d5d275c944a49ddbc539dd3435a84abc Mon Sep 17 00:00:00 2001 From: yennanliu Date: Tue, 5 Dec 2023 18:27:20 +0800 Subject: [PATCH] add productService test --- .../ShoppingCart/service/ProductService.java | 1 - .../service/ProductServiceTest.java | 94 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 ShoppingCart/Backend/src/test/java/com/yen/ShoppingCart/service/ProductServiceTest.java diff --git a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/ProductService.java b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/ProductService.java index be0179ad3..5aafc9290 100644 --- a/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/ProductService.java +++ b/ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/service/ProductService.java @@ -22,7 +22,6 @@ public void addProduct(ProductDto productDto, Category category) { Product product = getProductFromDto(productDto, category); productRepository.save(product); - } // local method diff --git a/ShoppingCart/Backend/src/test/java/com/yen/ShoppingCart/service/ProductServiceTest.java b/ShoppingCart/Backend/src/test/java/com/yen/ShoppingCart/service/ProductServiceTest.java new file mode 100644 index 000000000..86a978449 --- /dev/null +++ b/ShoppingCart/Backend/src/test/java/com/yen/ShoppingCart/service/ProductServiceTest.java @@ -0,0 +1,94 @@ +package com.yen.ShoppingCart.service; + +import com.yen.ShoppingCart.model.Category; +import com.yen.ShoppingCart.model.Product; +import com.yen.ShoppingCart.model.dto.product.ProductDto; +import com.yen.ShoppingCart.repository.ProductRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class ProductServiceTest { + + @Mock + ProductRepository productRepository; + + @InjectMocks + ProductService productService; + + Product product; + + ProductDto productDto; + + Category category; + + List productList; + + @BeforeEach + public void setUp(){ + + System.out.println("setup ..."); + // public Product(String name, String imageURL, double price, String description, Category category) { + Category category = new Category(); + product = new Product("prod_name","img_url", 100.0, "some desp", category); + productDto = new ProductDto(product); + + productList = new ArrayList<>(); + for (int i = 0; i < 3; i++){ + productList.add(product); + } + } + + @Test + public void testAddProductSuccess(){ + + // TODO : adjust test logic + Mockito.when(productRepository.save(product)).thenReturn(product); + productService.addProduct(productDto, category); + } + + @Test + public void testGetProductFromDto(){ + + Product receivedProd = ProductService.getProductFromDto(productDto, category); + assertEquals(receivedProd.getName(), "prod_name"); + assertEquals(receivedProd.getImageURL(), "img_url"); + assertEquals(receivedProd.getPrice(), 100.0); + assertEquals(receivedProd.getDescription(), "some desp"); + assertEquals(receivedProd.getCategory(), null); + } + + @Test + public void ShouldReturnProductList(){ + + Mockito.when(productRepository.findAll()).thenReturn(productList); + List res = productService.listProducts(); + assertEquals(res.size(), 3); + assertEquals(res.get(0).getPrice(), 100.0); + assertEquals(res.get(0).getName(), "prod_name"); + assertEquals(res.get(0).getImageURL(), "img_url"); + assertEquals(res.get(0).getDescription(), "some desp"); + } + + @Test + public void ShouldReturnNullIfNullProductList(){ + + Mockito.when(productRepository.findAll()).thenReturn(new ArrayList<>()); + List res = productService.listProducts(); + assertEquals(res.size(), 0); + } + +} \ No newline at end of file