@@ -192,6 +192,70 @@ void testPlaceOrderInsufficientStock() {
192192 });
193193 assertEquals ("Not enough stock for product 1" , exception .getMessage ());
194194}
195+ @ Test
196+ void testProductConAddProduct () {
197+ ProductRepo mockProductRepo = mock (ProductRepo .class );
198+
199+ Products mockProduct = new Products (1L , "Smartphone" , "High-end smartphone" , 1000.00 , 20 );
200+
201+ when (mockProductRepo .save (any (Products .class ))).thenReturn (mockProduct );
202+
203+ ProductCon productCon = new ProductCon (mockProductRepo );
204+
205+ Products savedProduct = productCon .addProduct (mockProduct );
206+
207+ verify (mockProductRepo , times (1 )).save (any (Products .class ));
208+
209+ assertNotNull (savedProduct );
210+ assertEquals ("Smartphone" , savedProduct .getName ());
211+ assertEquals ("High-end smartphone" , savedProduct .getDescription ());
212+ assertEquals (1000.00 , savedProduct .getPrice ());
213+ assertEquals (20 , savedProduct .getStock ());
214+ }
215+
216+ @ Test
217+ void testRun () {
218+ String simulatedInput = """
219+ 1
220+ John Doe
221+ 222+ password123
223+ 2
224+ Laptop
225+ Gaming Laptop
226+ 1200.50
227+ 10
228+ 3
229+ 1
230+ 1
231+ 2
232+ 4
233+ """ ; // Exit
234+ System .setIn (new ByteArrayInputStream (simulatedInput .getBytes (StandardCharsets .UTF_8 )));
235+
236+ ByteArrayOutputStream outputTest = new ByteArrayOutputStream ();
237+ System .setOut (new PrintStream (outputTest , true , StandardCharsets .UTF_8 ));
238+
239+ when (
userService .
registerUser (
any (
User .
class ))).
thenReturn (
new User (
1L ,
"John Doe" ,
"[email protected] " ,
"password123" ));
240+ when (productService .addProduct (any (Products .class ))).thenReturn (new Products (1L , "Laptop" , "Gaming Laptop" , 1200.50 , 10 ));
241+ when (
orderService .
placeOrder (
anyLong (),
anyLong (),
anyInt ())).
thenReturn (
new Orders (
1L ,
new User (
1L ,
"John Doe" ,
"[email protected] " ,
"password123" ),
new Products (
1L ,
"Laptop" ,
"Gaming Laptop" ,
1200.50 ,
10 ),
5 ,
6002.50 ));
242+
243+ ecommerceApp .run ();
244+
245+ verify (userService , times (1 )).registerUser (any (User .class ));
246+ verify (productService , times (1 )).addProduct (any (Products .class ));
247+ verify (orderService , times (1 )).placeOrder (anyLong (), anyLong (), anyInt ());
248+
249+ String output = outputTest .toString (StandardCharsets .UTF_8 );
250+ assertTrue (output .contains ("Welcome to the Monolithic E-commerce CLI!" ));
251+ assertTrue (output .contains ("Choose an option:" ));
252+ assertTrue (output .contains ("Register User" ));
253+ assertTrue (output .contains ("Add Product" ));
254+ assertTrue (output .contains ("Place Order" ));
255+ assertTrue (output .contains ("Exiting the application. Goodbye!" ));
256+ }
257+
258+
195259
196260
197261}
0 commit comments