Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need spring boot jpa based java code for this controller test cases to pass. Below has given test case file please provide the solution
I need spring boot jpa based java code for this controller test cases to pass. Below has given test case file please provide the solution as soon as possible.
package com.stackroute.springdatajpamysql.test;
import com.stackroute.springdatajpamysql.controller.ProductController;
import com.stackroute.springdatajpamysql.entity.Product;
import com.stackroute.springdatajpamysql.service.ProductService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.httpHttpStatus;
import org.springframework.httpResponseEntity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.samePropertyValuesAs;
import static org.junit.jupiter.api.Assertions.;
import static org.mockito.Mockito.;
public class ProductControllerTest
@InjectMocks
private ProductController productController;
@Mock
private ProductService productService;
@BeforeEach
public void init
MockitoAnnotations.openMocksthis;
@Test
public void testGetAllProducts
whenproductServicegetAllProductsthenReturnsomeListOfProducts;
ResponseEntity response productController.getAllProducts;
verifyproductService timesgetAllProducts;
assertThatresponsegetBody samePropertyValuesAssomeListOfProducts;
@Test
public void testGetProductById
Long productId L;
Product expectedProduct new ProductproductId "Sample Product", ;
whenproductServicegetProductByIdproductIdthenReturnexpectedProduct;
ResponseEntity response productController.getProductByIdproductId;
verifyproductService timesgetProductByIdproductId;
assertEqualsHttpStatusOK response.getStatusCode;
Use Hamcrest's samePropertyValuesAs to compare objects
assertThatresponsegetBody samePropertyValuesAsexpectedProduct;
@Test
public void testSaveProduct
Product product someProduct;
whenproductServicesaveProductproductthenReturnproduct;
ResponseEntity response productController.saveProductproduct;
verifyproductService timessaveProductproduct;
assertEqualsHttpStatusOK response.getStatusCode;
assertEqualsproduct response.getBody;
@Test
public void testUpdateProduct
Long productId L;
Product updatedProduct someProduct;
whenproductServiceupdateProductupdatedProduct productIdthenReturnupdatedProduct;
ResponseEntity response productController.updateProductupdatedProduct productId;
verifyproductService timesupdateProductupdatedProduct productId;
assertEqualsHttpStatusOK response.getStatusCode;
assertEqualsupdatedProduct response.getBody;
@Test
public void testDeleteProduct
Long productId L;
whenproductServicedeleteProductproductIdthenReturnProduct Deleted";
ResponseEntity response productController.deleteProductproductId;
verifyproductService timesdeleteProductproductId;
assertEqualsHttpStatusOK response.getStatusCode;
assertEqualsProduct Deleted", response.getBody;
@Test
public void testGetAllProductsHavingPriceLessThan
Mocking data
double price ;
Product product new ProductL "Product;
Product product new ProductL "Product;
List productList Arrays.asListproduct product;
Mocking the service method
whenproductServicegetAllProductsHavingPriceLessThanpricethenReturnproductList;
Calling the controller method
ResponseEntity responseEntity productController.getAllProductsHavingPriceLessThanprice;
Verifying the response status
assertNotNullresponseEntity;
assertEqualsHttpStatusOK responseEntity.getStatusCode;
Verifying the response body
assertNotNullresponseEntitygetBody;
assertTrueresponseEntitygetBody instanceof List;
List result List responseEntity.getBody;
assertEquals result.size;
assertEqualsProduct result.getgetName;
assertEquals result.getgetPrice;
assertEqualsProduct result.getgetName;
assertEquals result.getgetPrice;
Verifying that the service method was called
verifyproduct
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started