Skip to content

Commit c8c63a4

Browse files
committed
refactor: Refactored test methods
- refactored static entity codes to methods
1 parent 3184fa3 commit c8c63a4

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/test/java/com/f_lab/la_planete/domain/FoodTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class FoodTest {
1414
@DisplayName("음식 수량을 줄였을 때 성공")
1515
void test_minus_food_quantity_success() {
1616
// given
17-
Food food = new Food();
18-
food.setPrice(BigDecimal.valueOf(1000));
19-
food.setTotalQuantity(10);
17+
BigDecimal price = BigDecimal.valueOf(1000);
18+
int quantity = 10;
19+
Food food = createFood(price, quantity);
2020

2121
// when
2222
food.minusQuantity(5);
@@ -29,12 +29,19 @@ void test_minus_food_quantity_success() {
2929
@DisplayName("음식 수량을 줄였을 때 수량보다 많아서 실패할 때 예외 던짐")
3030
void test_minus_food_quantity_fail() {
3131
// given
32-
Food food = new Food();
33-
food.setPrice(BigDecimal.valueOf(1000));
34-
food.setTotalQuantity(10);
32+
BigDecimal price = BigDecimal.valueOf(1000);
33+
int quantity = 10;
34+
Food food = createFood(price, quantity);
3535

3636
// then
3737
assertThatThrownBy(() -> food.minusQuantity(11))
3838
.isInstanceOf(IllegalStateException.class);
3939
}
40+
41+
private Food createFood(BigDecimal price, int quantity) {
42+
return Food.builder()
43+
.price(price)
44+
.totalQuantity(quantity)
45+
.build();
46+
}
4047
}

src/test/java/com/f_lab/la_planete/domain/OrderTest.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,52 @@ class OrderTest {
2525
.totalQuantity(1000)
2626
.build();
2727

28-
Voucher voucher = Voucher.builder()
29-
.discountRate(BigDecimal.valueOf(0.45))
30-
.expiryDate(LocalDateTime.of(3000, Month.DECEMBER, 12, 23, 59))
31-
.build();
32-
3328
@Test
3429
@DisplayName("할인 쿠폰이 있는 경우에 calculateTotalCost() 메서드")
3530
void test_totalCost_given_food_and_voucher_success() {
3631
// given
37-
BigDecimal totalCost = food.calculateCost(3);
38-
Order order = Order.builder()
39-
.food(food)
40-
.totalCost(totalCost)
41-
.quantity(3)
42-
.voucher(voucher)
43-
.build();
32+
int quantity = 3;
33+
BigDecimal totalCost = food.calculateCost(quantity);
34+
Voucher voucher = createVoucher();
35+
Order order = createOrder(totalCost, quantity, voucher);
4436

4537
// when
4638
BigDecimal calculatedTotalCost = order.calculateTotalCost();
4739

4840
// then
49-
Assertions.assertThat(calculatedTotalCost).isEqualTo(BigDecimal.valueOf(16500));
41+
BigDecimal NUM_16500 = BigDecimal.valueOf(16500);
42+
Assertions.assertThat(calculatedTotalCost).isEqualTo(NUM_16500);
5043
}
5144

5245
@Test
5346
@DisplayName("할인 쿠폰이 없는 경우에 calculateTotalCost() 메서드")
5447
void test_totalCost_given_food_and_without_voucher_success() {
5548
// given
56-
BigDecimal totalCost = food.calculateCost(3);
57-
Order order = Order.builder()
58-
.food(food)
59-
.totalCost(totalCost)
60-
.quantity(3)
61-
.build();
49+
int quantity = 3;
50+
BigDecimal totalCost = food.calculateCost(quantity);
51+
Order order = createOrder(totalCost, quantity, null);
6252

6353
// when
6454
BigDecimal calculatedTotalCost = order.calculateTotalCost();
6555

6656
// then
67-
Assertions.assertThat(calculatedTotalCost).isEqualTo(BigDecimal.valueOf(30000));
57+
BigDecimal NUM_30000 = BigDecimal.valueOf(30000);
58+
Assertions.assertThat(calculatedTotalCost).isEqualTo(NUM_30000);
59+
}
60+
61+
private Order createOrder(BigDecimal totalCost, int quantity, Voucher voucher) {
62+
return Order.builder()
63+
.food(food)
64+
.totalCost(totalCost)
65+
.quantity(quantity)
66+
.voucher(voucher)
67+
.build();
68+
}
69+
70+
private Voucher createVoucher() {
71+
return Voucher.builder()
72+
.discountRate(BigDecimal.valueOf(0.45))
73+
.expiryDate(LocalDateTime.of(3000, Month.DECEMBER, 12, 23, 59))
74+
.build();
6875
}
6976
}

src/test/java/com/f_lab/la_planete/domain/VoucherTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class VoucherTest {
2828
@DisplayName("0 ~ 100 사이의 값이 들어올 때 정상적으로 할인이 적용된 값을 return")
2929
void test_voucher_discount_success() {
3030
// given
31-
Voucher voucher = new Voucher();
32-
voucher.setDiscountRate(BigDecimal.valueOf(0.25));
31+
BigDecimal discountRate = BigDecimal.valueOf(0.25);
32+
Voucher voucher = createVoucher(discountRate);
3333

3434
// when
3535
BigDecimal afterDiscountsGBP1 = voucher.apply(BigDecimal.valueOf(100), GBP);
@@ -46,13 +46,18 @@ void test_voucher_discount_success() {
4646
@DisplayName("currency 값에 null 값이 들어갔을 경우에 예외를 던진다.")
4747
void test_voucher_discount_failure() {
4848
// given
49-
Voucher voucher = new Voucher();
50-
voucher.setDiscountRate(BigDecimal.valueOf(0.25));
49+
BigDecimal discountRate = BigDecimal.valueOf(0.25);
50+
Voucher voucher = createVoucher(discountRate);
5151

5252
// when
5353
assertThatThrownBy(() ->
5454
voucher.apply(BigDecimal.valueOf(100), null)
5555
).isInstanceOf(IllegalStateException.class);
56+
}
5657

58+
private Voucher createVoucher(BigDecimal discountRate) {
59+
return Voucher.builder()
60+
.discountRate(discountRate)
61+
.build();
5762
}
5863
}

0 commit comments

Comments
 (0)