Skip to content

Commit

Permalink
refactor: Refactored test methods
Browse files Browse the repository at this point in the history
- refactored static entity codes to methods
  • Loading branch information
koreanMike513 committed Jan 5, 2025
1 parent 3184fa3 commit 49a7753
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
19 changes: 13 additions & 6 deletions src/test/java/com/f_lab/la_planete/domain/FoodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class FoodTest {
@DisplayName("음식 수량을 줄였을 때 성공")
void test_minus_food_quantity_success() {
// given
Food food = new Food();
food.setPrice(BigDecimal.valueOf(1000));
food.setTotalQuantity(10);
BigDecimal price = BigDecimal.valueOf(1000);
int quantity = 10;
Food food = createFood(price, quantity);

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

// then
assertThatThrownBy(() -> food.minusQuantity(11))
.isInstanceOf(IllegalStateException.class);
}

private Food createFood(BigDecimal price, int quantity) {
return Food.builder()
.price(price)
.totalQuantity(quantity)
.build();
}
}
47 changes: 27 additions & 20 deletions src/test/java/com/f_lab/la_planete/domain/OrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,52 @@ class OrderTest {
.totalQuantity(1000)
.build();

Voucher voucher = Voucher.builder()
.discountRate(BigDecimal.valueOf(0.45))
.expiryDate(LocalDateTime.of(3000, Month.DECEMBER, 12, 23, 59))
.build();

@Test
@DisplayName("할인 쿠폰이 있는 경우에 calculateTotalCost() 메서드")
void test_totalCost_given_food_and_voucher_success() {
// given
BigDecimal totalCost = food.calculateCost(3);
Order order = Order.builder()
.food(food)
.totalCost(totalCost)
.quantity(3)
.voucher(voucher)
.build();
int quantity = 3;
BigDecimal totalCost = food.calculateCost(quantity);
Voucher voucher = createVoucher();
Order order = createOrder(totalCost, quantity, voucher);

// when
BigDecimal calculatedTotalCost = order.calculateTotalCost();

// then
Assertions.assertThat(calculatedTotalCost).isEqualTo(BigDecimal.valueOf(16500));
BigDecimal NUM_16500 = BigDecimal.valueOf(16500);
Assertions.assertThat(calculatedTotalCost).isEqualTo(NUM_16500);
}

@Test
@DisplayName("할인 쿠폰이 없는 경우에 calculateTotalCost() 메서드")
void test_totalCost_given_food_and_without_voucher_success() {
// given
BigDecimal totalCost = food.calculateCost(3);
Order order = Order.builder()
.food(food)
.totalCost(totalCost)
.quantity(3)
.build();
int quantity = 3;
BigDecimal totalCost = food.calculateCost(quantity);
Order order = createOrder(totalCost, quantity, null);

// when
BigDecimal calculatedTotalCost = order.calculateTotalCost();

// then
Assertions.assertThat(calculatedTotalCost).isEqualTo(BigDecimal.valueOf(30000));
BigDecimal NUM_30000 = BigDecimal.valueOf(30000);
Assertions.assertThat(calculatedTotalCost).isEqualTo(NUM_30000);
}

private Order createOrder(BigDecimal totalCost, int quantity, Voucher voucher) {
return Order.builder()
.food(food)
.totalCost(totalCost)
.quantity(quantity)
.voucher(voucher)
.build();
}

private Voucher createVoucher() {
return Voucher.builder()
.discountRate(BigDecimal.valueOf(0.45))
.expiryDate(LocalDateTime.of(3000, Month.DECEMBER, 12, 23, 59))
.build();
}
}
13 changes: 9 additions & 4 deletions src/test/java/com/f_lab/la_planete/domain/VoucherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class VoucherTest {
@DisplayName("0 ~ 100 사이의 값이 들어올 때 정상적으로 할인이 적용된 값을 return")
void test_voucher_discount_success() {
// given
Voucher voucher = new Voucher();
voucher.setDiscountRate(BigDecimal.valueOf(0.25));
BigDecimal discountRate = BigDecimal.valueOf(0.25);
Voucher voucher = createVoucher(discountRate);

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

// when
assertThatThrownBy(() ->
voucher.apply(BigDecimal.valueOf(100), null)
).isInstanceOf(IllegalStateException.class);
}

private Voucher createVoucher(BigDecimal discountRate) {
return Voucher.builder()
.discountRate(discountRate)
.build();
}
}

0 comments on commit 49a7753

Please sign in to comment.