forked from kappsegla/MockingExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingCartTest.java
More file actions
108 lines (94 loc) · 3.16 KB
/
ShoppingCartTest.java
File metadata and controls
108 lines (94 loc) · 3.16 KB
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
package com.example.shop;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
class ShoppingCartTest {
@Test
void addItem_returnsTrue_whenItemIsAdded() {
ShoppingCart cart = new ShoppingCart();
boolean result = cart.addItem("T-Shirt", 129.0, 1);
assertTrue(result);
}
@Test
void removeItem_returnsTrue_whenItemExists() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100.0, 1);
boolean result = cart.removeItem("T-Shirt");
assertTrue(result);
}
@Test
void getTotalPrice_returnsSumOfPrices() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100.0, 2);
cart.addItem("Jeans", 300, 1);
assertEquals(500, cart.getTotalPrice());
}
@Test
void updateQuantity_returnsTrue_andUpdatesTotalPrice() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100, 1);
boolean result = cart.updateQuantity("T-Shirt", 3);
assertTrue(result);
assertEquals(300.0, cart.getTotalPrice());
}
@Test
void applyDiscount_returnsTrue_andReducesTotal() {
ShoppingCart cart = new ShoppingCart();
cart.addItem("T-Shirt", 100.0, 1);
boolean result = cart.applyDiscount(0.3);
assertTrue(result);
assertEquals(70.0, cart.getTotalPrice());
}
@Test
void removeItemThatDoesNotExist_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.removeItem("Hat"));
}
@Test
void removeItemWhenNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.removeItem(null));
}
@ParameterizedTest
@CsvSource({
"-1.0, 1",
"10.0, -1",
"10.0, 0",
"-10.0, -1"
})
void addItemWithNegativePriceOrQty_returnsFalse(double price, int quantity) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.addItem("T-Shirt", price, quantity));
}
@Test
void addItemWhereNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.addItem(null, 100,1));
}
@Test
void updateQuantityWhereNameIsNull_returnsFalse() {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.updateQuantity(null, 1));
}
@ParameterizedTest
@CsvSource({
"-1",
"0"
})
void updateQuantityWhereQtyIsNull_returnsFalse(int quantity) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.updateQuantity("T-Shirt", quantity ));
}
@ParameterizedTest
@CsvSource({
"-0.1",
"1.1"
})
void applyDiscountWhereDiscountIsNotAllowed_returnsFalse(double discount) {
ShoppingCart cart = new ShoppingCart();
Assertions.assertFalse(cart.applyDiscount(discount));
}
}