Skip to content
Snippets Groups Projects
Commit f6091c78 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

fixed test

parent 3f88bbe2
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.controller.product;
import ntnu.idatt2016.v233.SmartMat.dto.request.ProductRequest;
import ntnu.idatt2016.v233.SmartMat.entity.product.Category;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.service.product.CategoryService;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
......@@ -16,6 +18,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
......@@ -24,6 +27,9 @@ public class ProductControllerTest {
@Mock
private ProductService productService;
@Mock
private CategoryService categoryService;
@InjectMocks
private ProductController productController;
......@@ -33,12 +39,15 @@ public class ProductControllerTest {
ProductRequest productRequest = ProductRequest.builder()
.ean(123L)
.name("Test Product")
.description("A test product")
.description("A test product kylling")
.image("http://test.com/image.jpg")
.build();
when(productService.getProductById(123L)).thenReturn(Optional.empty());
when(productService.getProductVolume(123L)).thenReturn(Optional.of(List.of("1", "kg")));
when(categoryService.getCategoryByName(anyString())).thenReturn(Optional.of(Category.builder()
.categoryName("Kjøtt")
.build()));
// Act
ResponseEntity<Product> response = productController.createProduct(productRequest);
......@@ -48,7 +57,7 @@ public class ProductControllerTest {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(123L, product.getEan());
assertEquals("Test Product", product.getName());
assertEquals("A test product", product.getDescription());
assertEquals("A test product kylling", product.getDescription());
assertEquals("http://test.com/image.jpg", product.getUrl());
assertEquals("kg", product.getUnit());
assertEquals(1.0, product.getAmount());
......@@ -60,11 +69,14 @@ public class ProductControllerTest {
ProductRequest productRequest = ProductRequest.builder()
.ean(123L)
.name("Test Product")
.description("A test product")
.description("A test product kylling")
.image("http://test.com/image.jpg")
.build();
when(productService.getProductById(123L)).thenReturn(Optional.of(new Product()));
when(categoryService.getCategoryByName(anyString())).thenReturn(Optional.of(Category.builder()
.categoryName("Kjøtt")
.build()));
// Act
ResponseEntity<Product> response = productController.createProduct(productRequest);
......@@ -80,6 +92,7 @@ public class ProductControllerTest {
Product product = new Product();
product.setEan(123L);
product.setName("Test Product");
product.setDescription("A test kylling");
when(productService.getProductById(123L)).thenReturn(Optional.of(product));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment