Skip to content

Commit e926e60

Browse files
author
Mohaned Atef
committed
fixed testing class
1 parent 31222a1 commit e926e60

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.iluwatar.monolithic;
2+
3+
import com.iluwatar.monolithic.controller.OrderCon;
4+
import com.iluwatar.monolithic.controller.ProductCon;
5+
import com.iluwatar.monolithic.controller.UserCon;
6+
import com.iluwatar.monolithic.model.Products;
7+
import com.iluwatar.monolithic.model.Orders;
8+
import com.iluwatar.monolithic.model.User;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.Mock;
12+
import org.mockito.MockitoAnnotations;
13+
14+
import java.io.ByteArrayInputStream;
15+
import java.io.ByteArrayOutputStream;
16+
import java.io.PrintStream;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.Scanner;
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
22+
import static org.mockito.Mockito.*;
23+
24+
class MonolithicAppTest {
25+
26+
@Mock
27+
private UserCon userService;
28+
29+
@Mock
30+
private ProductCon productService;
31+
32+
@Mock
33+
private OrderCon orderService;
34+
35+
private EcommerceApp ecommerceApp;
36+
37+
private ByteArrayOutputStream outputStream;
38+
39+
@BeforeEach
40+
void setUp() {
41+
MockitoAnnotations.openMocks(this);
42+
ecommerceApp = new EcommerceApp(userService, productService, orderService);
43+
outputStream = new ByteArrayOutputStream();
44+
System.setOut(new PrintStream(outputStream, true, StandardCharsets.UTF_8));
45+
}
46+
47+
@Test
48+
void testRegisterUser() {
49+
// Simulate user input for registering a user
50+
String simulatedInput = "John Doe\n[email protected]\npassword123\n";
51+
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes(StandardCharsets.UTF_8)));
52+
53+
ecommerceApp.registerUser(new Scanner(System.in, StandardCharsets.UTF_8));
54+
55+
// Verify userService interaction
56+
verify(userService, times(1)).registerUser(any(User.class));
57+
assertTrue(outputStream.toString().contains("User registered successfully!"));
58+
}
59+
60+
@Test
61+
void testAddProduct() {
62+
// Simulate user input for adding a product
63+
String simulatedInput = "Laptop\nGaming Laptop\n1200.50\n10\n";
64+
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes(StandardCharsets.UTF_8)));
65+
66+
ecommerceApp.addProduct(new Scanner(System.in, StandardCharsets.UTF_8));
67+
68+
// Verify productService interaction
69+
verify(productService, times(1)).addProduct(any(Products.class));
70+
assertTrue(outputStream.toString().contains("Product added successfully!"));
71+
}
72+
73+
@Test
74+
void testPlaceOrderSuccess() {
75+
// Simulate user input for placing an order
76+
String simulatedInput = "1\n2\n3\n";
77+
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes(StandardCharsets.UTF_8)));
78+
79+
// Mocking placeOrder to return an order object
80+
Orders mockOrder = new Orders(); // Create a mock or dummy Orders object
81+
doReturn(mockOrder).when(orderService).placeOrder(anyLong(), anyLong(), anyInt());
82+
83+
ecommerceApp.placeOrder(new Scanner(System.in, StandardCharsets.UTF_8));
84+
85+
// Verify orderService interaction
86+
verify(orderService, times(1)).placeOrder(anyLong(), anyLong(), anyInt());
87+
assertTrue(outputStream.toString().contains("Order placed successfully!"));
88+
}
89+
90+
@Test
91+
void testPlaceOrderFailure() {
92+
// Simulate user input for placing an order
93+
String simulatedInput = "1\n2\n3\n";
94+
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes(StandardCharsets.UTF_8)));
95+
96+
doThrow(new RuntimeException("Product out of stock"))
97+
.when(orderService).placeOrder(anyLong(), anyLong(), anyInt());
98+
99+
ecommerceApp.placeOrder(new Scanner(System.in, StandardCharsets.UTF_8));
100+
101+
// Verify orderService interaction
102+
verify(orderService, times(1)).placeOrder(anyLong(), anyLong(), anyInt());
103+
assertTrue(outputStream.toString().contains("Error placing order: Product out of stock"));
104+
}
105+
}

0 commit comments

Comments
 (0)