-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathProductControllerTest.java
126 lines (105 loc) · 4.81 KB
/
ProductControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.javatechie.crud.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.javatechie.crud.example.controller.ProductController;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Arrays;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(ProductController.class)
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ProductService productService;
//added tests
@Autowired
private ObjectMapper objectMapper;
private Product product1;
private Product product2;
private List<Product> productList;
@BeforeEach
public void setUp() {
product1 = new Product(1, "Laptop", 1200, 10);
product2 = new Product(2, "Smartphone", 800, 25);
productList = Arrays.asList(product1, product2);
}
@Test
public void testAddProduct() throws Exception {
when(productService.saveProduct(product1)).thenReturn(product1);
mockMvc.perform(post("/addProduct")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(product1)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(product1.getId()))
.andExpect(jsonPath("$.name").value(product1.getName()))
.andExpect(jsonPath("$.price").value(product1.getPrice()))
.andExpect(jsonPath("$.quantity").value(product1.getQuantity()));
}
@Test
public void testAddProducts() throws Exception {
when(productService.saveProducts(productList)).thenReturn(productList);
mockMvc.perform(post("/addProducts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(productList)))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(product1.getId()))
.andExpect(jsonPath("$[1].id").value(product2.getId()));
}
@Test
public void testFindAllProducts() throws Exception {
when(productService.getProducts()).thenReturn(productList);
mockMvc.perform(get("/products")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value(product1.getId()))
.andExpect(jsonPath("$[1].id").value(product2.getId()));
}
@Test
public void testFindProductById() throws Exception {
when(productService.getProductById(1)).thenReturn(product1);
mockMvc.perform(get("/productById/{id}", 1)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(product1.getId()))
.andExpect(jsonPath("$.name").value(product1.getName()));
}
@Test
public void testFindProductByName() throws Exception {
when(productService.getProductByName("Laptop")).thenReturn(product1);
mockMvc.perform(get("/product/Laptop")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value(product1.getName()));
}
@Test
public void testUpdateProduct() throws Exception {
product1.setName("Updated Laptop");
when(productService.updateProduct(product1)).thenReturn(product1);
mockMvc.perform(put("/update")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(product1)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("Updated Laptop"));
}
@Test
public void testDeleteProduct() throws Exception {
when(productService.deleteProduct(1)).thenReturn("Product removed !! " + product1.getId());
mockMvc.perform(delete("/delete/{id}", 1)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("Product removed !! 1"));
}
}