55import com .smalltrend .service .products .ProductService ;
66import org .junit .jupiter .api .BeforeEach ;
77import org .junit .jupiter .api .Test ;
8+ import org .junit .jupiter .api .DisplayName ;
89import org .junit .jupiter .api .extension .ExtendWith ;
910import org .mockito .Mock ;
1011import org .mockito .junit .jupiter .MockitoExtension ;
1112import org .springframework .http .HttpStatus ;
1213import org .springframework .http .ResponseEntity ;
1314
14- import java .time .LocalDateTime ;
1515import java .util .List ;
1616
17- import static org .junit .jupiter .api .Assertions .assertEquals ;
18- import static org .junit .jupiter .api .Assertions .assertNotNull ;
19- import static org .mockito .ArgumentMatchers .any ;
20- import static org .mockito .ArgumentMatchers .eq ;
21- import static org .mockito .Mockito .never ;
22- import static org .mockito .Mockito .verify ;
23- import static org .mockito .Mockito .when ;
17+ import static org .junit .jupiter .api .Assertions .*;
18+ import static org .mockito .Mockito .*;
2419
2520/**
26- * Test class cho ProductController
27- * Kiểm tra các endpoint API liên quan đến sản phẩm
21+ * Unit Test for ProductController
2822 */
2923@ ExtendWith (MockitoExtension .class )
3024class ProductControllerTest {
@@ -35,204 +29,230 @@ class ProductControllerTest {
3529 private ProductController productController ;
3630
3731 @ BeforeEach
38- void setUp () {
32+ void setup () {
3933 productController = new ProductController (productService , null , null , null );
4034 }
4135
42- // ========== GET ALL TESTS ==========
36+ // ================= GET ALL ======= ==========
4337 @ Test
44- void getAll_shouldReturnOk () {
45- List <ProductResponse > expected = List .of (
38+ void getAll_shouldReturnProductList () {
39+
40+ // Arrange
41+ List <ProductResponse > products = List .of (
4642 ProductResponse .builder ()
4743 .id (1 )
4844 .name ("Coca-Cola 330ml" )
4945 .brand_name ("Coca-Cola" )
5046 .category_name ("Đồ uống" )
5147 .is_active (true )
52- .build ()
53- );
54- when (productService .getAll ()).thenReturn (expected );
48+ .build ());
49+
50+ when (productService .getAll ()).thenReturn (products );
5551
52+ // Act
5653 ResponseEntity <List <ProductResponse >> response = productController .getAll ();
5754
55+ // Assert
5856 assertEquals (HttpStatus .OK , response .getStatusCode ());
59- assertEquals (expected , response .getBody ());
57+ assertEquals (products , response .getBody ());
6058 verify (productService ).getAll ();
6159 }
6260
63- // ========== GET BY ID TESTS ==========
61+ // ================= GET BY ID ======= ==========
6462 @ Test
65- void getById_shouldReturnOk_whenProductExists () {
66- ProductResponse expected = ProductResponse .builder ()
63+ @ DisplayName ("Get product by id - success" )
64+ void getById_shouldReturnProduct_whenExists () {
65+
66+ ProductResponse product = ProductResponse .builder ()
6767 .id (5 )
68- .name ("Sữa Vinamilk 1L " )
68+ .name ("Sữa Vinamilk" )
6969 .brand_name ("Vinamilk" )
7070 .category_name ("Sữa" )
71- .is_active (true )
7271 .variant_count (2 )
72+ .is_active (true )
7373 .build ();
74- when (productService .getById (5 )).thenReturn (expected );
74+
75+ when (productService .getById (5 )).thenReturn (product );
7576
7677 ResponseEntity <ProductResponse > response = productController .getById (5 );
7778
7879 assertEquals (HttpStatus .OK , response .getStatusCode ());
79- assertEquals (expected , response .getBody ());
80+ assertEquals (product , response .getBody ());
81+
8082 verify (productService ).getById (5 );
8183 }
8284
8385 @ Test
84- void getById_shouldThrow_whenProductNotFound () {
85- // Kiểm tra khi sản phẩm không tồn tại
86- when (productService .getById (999 )).thenThrow (new RuntimeException ("Product not found with id: 999" ));
86+ void getById_shouldThrowException_whenProductNotFound () {
87+
88+ when (productService .getById (999 ))
89+ .thenThrow (new RuntimeException ("Product not found with id: 999" ));
8790
88- RuntimeException ex = org . junit . jupiter . api . Assertions . assertThrows (
91+ RuntimeException exception = assertThrows (
8992 RuntimeException .class ,
90- () -> productController .getById (999 ));
93+ () -> productController .getById (999 )
94+ );
9195
92- assertEquals ("Product not found with id: 999" , ex .getMessage ());
96+ assertEquals ("Product not found with id: 999" , exception .getMessage ());
9397 }
9498
95- // ========== CREATE TESTS ==========
99+ // ================= CREATE ======= ==========
96100 @ Test
97- void create_shouldReturnOk_whenValid () {
101+ void create_shouldCreateProductSuccessfully () {
102+
98103 CreateProductRequest request = CreateProductRequest .builder ()
99104 .name ("Nước cam Minute Maid" )
100105 .description ("Nước cam tươi" )
101- .categoryId (2 )
102106 .brandId (3 )
107+ .categoryId (2 )
103108 .isActive (true )
104109 .build ();
105- ProductResponse expected = ProductResponse .builder ()
110+
111+ ProductResponse responseData = ProductResponse .builder ()
106112 .id (10 )
107113 .name ("Nước cam Minute Maid" )
108114 .brand_id (3 )
109115 .category_id (2 )
110116 .is_active (true )
111117 .build ();
112- when (productService .create (request )).thenReturn (expected );
118+
119+ when (productService .create (request )).thenReturn (responseData );
113120
114121 ResponseEntity <ProductResponse > response = productController .create (request );
115122
116123 assertEquals (HttpStatus .OK , response .getStatusCode ());
117- assertEquals (expected , response .getBody ());
124+ assertEquals (responseData , response .getBody ());
118125 verify (productService ).create (request );
119126 }
120127
121128 @ Test
122- void create_shouldThrow_whenCategoryNotFound () {
123- // Kiểm tra khi danh mục không tồn tại
129+ void create_shouldThrowException_whenCategoryNotFound () {
130+
124131 CreateProductRequest request = CreateProductRequest .builder ()
125- .name ("Product" )
132+ .name ("Test Product" )
126133 .categoryId (999 )
127134 .build ();
135+
128136 when (productService .create (request ))
129137 .thenThrow (new RuntimeException ("Category not found with id: 999" ));
130138
131- RuntimeException ex = org . junit . jupiter . api . Assertions . assertThrows (
139+ RuntimeException exception = assertThrows (
132140 RuntimeException .class ,
133141 () -> productController .create (request ));
134142
135- assertEquals ("Category not found with id: 999" , ex .getMessage ());
143+ assertEquals ("Category not found with id: 999" , exception .getMessage ());
136144 }
137145
138146 @ Test
139- void create_shouldThrow_whenBrandNotFound () {
140- // Kiểm tra khi thương hiệu không tồn tại
147+ void create_shouldThrowException_whenBrandNotFound () {
148+
141149 CreateProductRequest request = CreateProductRequest .builder ()
142- .name ("Product" )
150+ .name ("Test Product" )
143151 .brandId (999 )
144152 .build ();
153+
145154 when (productService .create (request ))
146155 .thenThrow (new RuntimeException ("Brand not found with id: 999" ));
147156
148- RuntimeException ex = org . junit . jupiter . api . Assertions . assertThrows (
157+ RuntimeException exception = assertThrows (
149158 RuntimeException .class ,
150159 () -> productController .create (request ));
151160
152- assertEquals ("Brand not found with id: 999" , ex .getMessage ());
161+ assertEquals ("Brand not found with id: 999" , exception .getMessage ());
153162 }
154163
155- // ========== UPDATE TESTS ==========
164+ // ================= UPDATE ======= ==========
156165 @ Test
157- void update_shouldReturnOk_whenValid () {
166+ void update_shouldUpdateProductSuccessfully () {
167+
158168 CreateProductRequest request = CreateProductRequest .builder ()
159169 .name ("Coca-Cola 500ml" )
160- .description ("Cập nhật " )
170+ .description ("Update " )
161171 .categoryId (2 )
162172 .isActive (true )
163173 .build ();
164- ProductResponse expected = ProductResponse .builder ()
174+
175+ ProductResponse responseData = ProductResponse .builder ()
165176 .id (1 )
166177 .name ("Coca-Cola 500ml" )
167178 .category_id (2 )
168179 .is_active (true )
169180 .build ();
170- when (productService .update (1 , request )).thenReturn (expected );
181+
182+ when (productService .update (1 , request )).thenReturn (responseData );
171183
172184 ResponseEntity <ProductResponse > response = productController .update (1 , request );
173185
174186 assertEquals (HttpStatus .OK , response .getStatusCode ());
175- assertEquals (expected , response .getBody ());
187+ assertEquals (responseData , response .getBody ());
188+
176189 verify (productService ).update (1 , request );
177190 }
178191
179192 @ Test
180- void update_shouldThrow_whenProductNotFound () {
181- // Kiểm tra khi sản phẩm không tồn tại
182- CreateProductRequest request = CreateProductRequest .builder ().name ("Test" ).build ();
193+ void update_shouldThrowException_whenProductNotFound () {
194+
195+ CreateProductRequest request = CreateProductRequest .builder ()
196+ .name ("Test" )
197+ .build ();
198+
183199 when (productService .update (999 , request ))
184200 .thenThrow (new RuntimeException ("Product not found with id: 999" ));
185201
186- RuntimeException ex = org . junit . jupiter . api . Assertions . assertThrows (
202+ RuntimeException exception = assertThrows (
187203 RuntimeException .class ,
188204 () -> productController .update (999 , request ));
189205
190- assertEquals ("Product not found with id: 999" , ex .getMessage ());
206+ assertEquals ("Product not found with id: 999" , exception .getMessage ());
191207 }
192208
193- // ========== TOGGLE STATUS TESTS ==========
209+ // ================= TOGGLE STATUS ======= ==========
194210 @ Test
195- void toggleStatus_shouldReturnOk () {
211+ void toggleStatus_shouldToggleProductStatus () {
212+
196213 ResponseEntity <String > response = productController .toggleStatus (5 );
197214
198215 assertEquals (HttpStatus .OK , response .getStatusCode ());
199216 assertEquals ("Product status toggled" , response .getBody ());
217+
200218 verify (productService ).toggleStatus (5 );
201219 }
202220
203- // ========== DELETE TESTS ==========
221+ // ================= DELETE ======= ==========
204222 @ Test
205- void delete_shouldReturnOk_whenValid () {
223+ void delete_shouldDeleteProductSuccessfully () {
224+
206225 ResponseEntity <String > response = productController .delete (10 );
207226
208227 assertEquals (HttpStatus .OK , response .getStatusCode ());
209228 assertEquals ("Product deleted" , response .getBody ());
229+
210230 verify (productService ).delete (10 );
211231 }
212232
213233 @ Test
214- void delete_shouldThrow_whenProductNotFound () {
215- // Kiểm tra khi sản phẩm không tồn tại
216- when (productService .delete (999 ))
217- .thenThrow (new RuntimeException ("Product not found with id: 999" ));
234+ void delete_shouldThrowException_whenProductNotFound () {
218235
219- RuntimeException ex = org .junit .jupiter .api .Assertions .assertThrows (
236+ doThrow (new RuntimeException ("Product not found with id: 999" ))
237+ .when (productService ).delete (999 );
238+
239+ RuntimeException exception = assertThrows (
220240 RuntimeException .class ,
221241 () -> productController .delete (999 ));
222242
223- assertEquals ("Product not found with id: 999" , ex .getMessage ());
243+ assertEquals ("Product not found with id: 999" , exception .getMessage ());
224244 }
225245
226246 @ Test
227- void delete_shouldThrow_whenProductTooOld () {
228- // Kiểm tra khi sản phẩm được tạo quá 2 phút
229- when ( productService . delete ( 5 ))
230- .thenThrow ( new RuntimeException ( "Sản phẩm đã tạo quá 2 phút, bạn không thể xoá sản phẩm này nữa!" ) );
247+ void delete_shouldThrowException_whenProductTooOld () {
248+
249+ doThrow ( new RuntimeException ( "Sản phẩm đã tạo quá 2 phút, bạn không thể xoá sản phẩm này nữa!" ))
250+ .when ( productService ). delete ( 5 );
231251
232- RuntimeException ex = org . junit . jupiter . api . Assertions . assertThrows (
252+ RuntimeException exception = assertThrows (
233253 RuntimeException .class ,
234254 () -> productController .delete (5 ));
235255
236- assertEquals ("Sản phẩm đã tạo quá 2 phút, bạn không thể xoá sản phẩm này nữa!" , ex .getMessage ());
256+ assertEquals ("Sản phẩm đã tạo quá 2 phút, bạn không thể xoá sản phẩm này nữa!" , exception .getMessage ());
237257 }
238258}
0 commit comments