-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
ShoppingCart/Backend/src/test/java/com/yen/ShoppingCart/service/ProductServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |