-
Notifications
You must be signed in to change notification settings - Fork 1
/
CartTestingSuite.java
86 lines (75 loc) · 3.03 KB
/
CartTestingSuite.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
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.*;
public class CartTestingSuite {
Cart testingCart;
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
@Before
public void initialize() {
testingCart = new Cart();
System.setOut(new PrintStream(outputStreamCaptor));
}
@Test
public void testAddItem() {
VendingItem testItem = new VendingItem(3, "🥑", "avocado", 2.50);
testingCart.addItem(testItem);
assertEquals(true, testingCart.getItems().containsKey(testItem));
}
@Test
public void testRemoveItem() {
VendingItem testItem = new VendingItem(3, "🥑", "avocado", 2.50);
testingCart.addItem(testItem);
testingCart.addItem(testItem);
assertEquals(true, testingCart.getItems().containsKey(testItem));
for(int i = 0; i < 2; ++i) testingCart.removeItem(testItem);
assertEquals(false, testingCart.removeItem(testItem));
}
@Test
public void testCalculateSubtotal() {
VendingItem testItemOne = new VendingItem(3, "🥑", "avocado", 2.50);
VendingItem testItemTwo = new VendingItem(5, "🍏", "apple", 2.00);
VendingItem testItemThree = new VendingItem(2, "🍌", "banana", 0.75);
for(int i = 0; i < 3; ++i){
testingCart.addItem(testItemOne);
}
testingCart.addItem(testItemTwo);
testingCart.addItem(testItemThree);
assertEquals(10.25, testingCart.calculateSubtotal(), 0.1);
}
@Test
public void testSalesTax() {
//Testing with MO sales tax (4.23%)
VendingItem testItemOne = new VendingItem(3, "🥑", "avocado", 2.50);
VendingItem testItemTwo = new VendingItem(5, "🍏", "apple", 2.00);
VendingItem testItemThree = new VendingItem(2, "🍌", "banana", 0.75);
for(int i = 0; i < 3; ++i){
testingCart.addItem(testItemOne);
}
testingCart.addItem(testItemTwo);
testingCart.addItem(testItemThree);
testingCart.setState(Cart.State.MO);
double expectedTotal = 10.68;
double actualTotal = testingCart.calculateSubtotal() * (100 + testingCart.getState().getTaxRate())/100.0;
assertEquals(expectedTotal, actualTotal, 0.1);
}
@Test
public void testViewCart() {
VendingItem testItemOne = new VendingItem(3, "🥑", "avocado", 2.50);
VendingItem testItemTwo = new VendingItem(5, "🍏", "apple", 2.00);
VendingItem testItemThree = new VendingItem(2, "🍌", "banana", 0.75);
for(int i = 0; i < 3; ++i){
testingCart.addItem(testItemOne);
}
testingCart.addItem(testItemTwo);
testingCart.addItem(testItemThree);
testingCart.viewCart(false);
assertNotNull(outputStreamCaptor);
}
@After
public void tearDown() {
System.setOut(standardOut);
}
}