Skip to content

Commit

Permalink
add productService test
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 5, 2023
1 parent 7343ed2 commit f04ec7a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public void addProduct(ProductDto productDto, Category category) {

Product product = getProductFromDto(productDto, category);
productRepository.save(product);

}

// local method
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Product> 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<ProductDto> 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<ProductDto> res = productService.listProducts();
assertEquals(res.size(), 0);
}

}

0 comments on commit f04ec7a

Please sign in to comment.