Skip to content

[4기 - 소재훈] Week2-1 바우처 관리 애플리케이션 테스트 코드 작성(재 PR 작업) #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: jay-so
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5e0f655
refactor: ConsoleApplication 리팩토링
jay-so Jul 3, 2023
5f8b98f
init: Voucher TestCode 세팅
jay-so Jul 3, 2023
02dfa0f
feat:Customer 클래스(Customer Domain Class) 구현
jay-so Jul 6, 2023
f1f9c5f
refactor:VoucherType의 오류 메시지 리팩토링
jay-so Jul 6, 2023
10b60ad
refactor:Command enum 리팩토링
jay-so Jul 6, 2023
ce917df
feat: VoucherType에 따른 고정 할인 바우처, 퍼센트 비율 할인 바우처의 지정 범위를 벗어나는 경우 에러 메시지…
jay-so Jul 6, 2023
2ba1de8
refactor:Console 클래스 리팩토링
jay-so Jul 6, 2023
5c158a1
refactor: ConsoleApplication 리팩토링
jay-so Jul 6, 2023
0d6236e
Rename: 각 Controller, Service, doamin 폴더 안의 Voucher패키지로 Voucher 관련 Co…
jay-so Jul 6, 2023
50faf9e
refactor: Output
jay-so Jul 6, 2023
b244133
feat: 고정할인 바우처(FixedDiscountVoucherTest) 테스트
jay-so Jul 7, 2023
3561696
feat: 비율할인 바우처(PercentDiscountVoucher) 테스트 코드
jay-so Jul 7, 2023
65775ed
feat: ConsoleApplication 테스트 코드
jay-so Jul 7, 2023
7b6afd7
feat:VoucherService 테스트 코드
jay-so Jul 7, 2023
fac02cf
feat:VoucherController 테스트 코드 작성
jay-so Jul 7, 2023
85a7f28
refactor:불 필요한 주석 제거
jay-so Jul 7, 2023
b4d1d4d
rename: Voucher Controller,Service,repository 폴더 위치 변경
jay-so Jul 8, 2023
b253bc7
refactor: VoucherService의 공백 제거
jay-so Jul 8, 2023
64686ea
refactor:ConsoleApplication 리팩토링
jay-so Jul 8, 2023
eff8ead
refactor: origin/w2-1 브랜치와 차이가 나는 로컬 w2-1 브랜치 수정
jay-so Jul 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions src/main/java/com/prgrms/springbootbasic/ConsoleApplication.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.prgrms.springbootbasic;

import com.prgrms.springbootbasic.controller.VoucherController;
import com.prgrms.springbootbasic.domain.Voucher;
import com.prgrms.springbootbasic.controller.voucher.VoucherController;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import com.prgrms.springbootbasic.enums.Command;
import com.prgrms.springbootbasic.enums.VoucherType;
import com.prgrms.springbootbasic.view.Console;
Expand All @@ -25,31 +25,41 @@ public void run(String... args) throws Exception {
console.consoleMenu();

while (true) {
String command = console.inputCommand();
Command inputCommand = Command.checkInputCommand(command);

switch (inputCommand) {
case CREATE -> createVoucher();
case LIST -> getVoucherList();
case EXIT -> {
console.printMessage("프로그램을 종료합니다.");
return;
try {
String command = console.inputCommand();
Command inputCommand = Command.of(command);

switch (inputCommand) {
case CREATE -> createVoucher();
case LIST -> getVoucherList();
case EXIT -> {
console.printMessage("프로그램을 종료합니다.");
return;
}
}
} catch (IllegalArgumentException e) {
log.error("명령어가 잘못 입력되었습니다. ", e.getMessage());
} catch (Exception e) {
log.error("프로그램에서 오류가 발생하였습니다.", e.getMessage());
Comment on lines +41 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stackTrace가 있다면 나중에 로그를 볼때 좀더 빠르게 파악할것 같아요.

}
}
}

private void createVoucher() {
public void createVoucher() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 처음보네요

String voucherTypeInput = console.inputVoucherType();
VoucherType voucherType = VoucherType.checkVoucherType(voucherTypeInput);
VoucherType voucherType = VoucherType.of(voucherTypeInput);

long voucherDiscount = console.inputVoucherDiscount();

voucherController.createVoucher(voucherType, voucherDiscount);
console.printMessage("바우처가 생성되었습니다!");
try {
voucherController.createVoucher(voucherType, voucherDiscount);
console.printMessage("바우처가 생성되었습니다!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

과연 생성 되었을까요?

} catch (IllegalArgumentException e) {
console.printMessage("생성할 바우처의 금액의 범위를 다시 한번 확인해주세요!");
}
}

private void getVoucherList() {
public void getVoucherList() {
Map<UUID, Voucher> voucherMap = voucherController.printVoucherList();
console.printlnVoucherList(voucherMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.prgrms.springbootbasic.controller;
package com.prgrms.springbootbasic.controller.voucher;

import com.prgrms.springbootbasic.domain.Voucher;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import com.prgrms.springbootbasic.enums.VoucherType;
import com.prgrms.springbootbasic.service.VoucherService;
import com.prgrms.springbootbasic.service.voucher.VoucherService;
import java.util.Map;
import java.util.UUID;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.springbootbasic.domain;
package com.prgrms.springbootbasic.domain.voucher;

import com.prgrms.springbootbasic.enums.VoucherType;
import java.util.UUID;
Expand All @@ -14,7 +14,7 @@ public FixedDiscountVoucher(long discount) {
this.voucherId = UUID.randomUUID();
this.discount = discount;
}

@Override
public VoucherType getVoucherType() {
return VoucherType.FIXED;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.springbootbasic.domain;
package com.prgrms.springbootbasic.domain.voucher;

import com.prgrms.springbootbasic.enums.VoucherType;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.springbootbasic.domain;
package com.prgrms.springbootbasic.domain.voucher;

import com.prgrms.springbootbasic.enums.VoucherType;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static Command of(String inputCommand) {
try {
return Command.valueOf(inputCommand.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 명령어 입력입니다. 다시 입력해주세요. " + inputCommand);
throw new IllegalArgumentException("잘못된 명령어 입력입니다. 프로그램 종료(exit),바우처 생성(create), 바우처 목록 조회(list) 중 하나를 입력해주세요.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static VoucherType of(String voucherType) {
try {
return VoucherType.valueOf(voucherType.toUpperCase());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("잘못된 바우처 타입입니다." + voucherType);
throw new IllegalArgumentException("잘못된 바우처 타입을 입력하셨습니다. 고정 할인 바우처(fixed) 또는 비율 할인 바우처(percent)를 선택해서 다시 입력해주세요!");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgrms.springbootbasic.repository;
package com.prgrms.springbootbasic.repository.voucher;

import com.prgrms.springbootbasic.domain.Voucher;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.prgrms.springbootbasic.repository.voucher;

public class VoucherJdbcRepository {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.prgrms.springbootbasic.repository.voucher;

import com.prgrms.springbootbasic.domain.voucher.Voucher;
import java.util.Map;
import java.util.UUID;

public interface VoucherRepository {

Voucher insert(Voucher voucher);

Map<UUID, Voucher> getAllVouchersList();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.prgrms.springbootbasic.service.voucher;

import com.prgrms.springbootbasic.domain.voucher.FixedDiscountVoucher;
import com.prgrms.springbootbasic.domain.voucher.PercentDiscountVoucher;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import com.prgrms.springbootbasic.enums.VoucherType;
import com.prgrms.springbootbasic.repository.voucher.VoucherRepository;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Service;

@Service
public class VoucherService {

private final VoucherRepository voucherRepository;

public VoucherService(VoucherRepository voucherRepository) {
this.voucherRepository = voucherRepository;
}

public Voucher createVoucher(VoucherType type, long discount) {
try {
isValidDiscount(type, discount);
Voucher voucher = switch (type) {
case FIXED -> new FixedDiscountVoucher(discount);
case PERCENT -> new PercentDiscountVoucher(discount);
};
return voucherRepository.insert(voucher);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
throw e;
}
Comment on lines +29 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root 레벨에서 해당 예외를 catch하고 있는데 중간에 한번 더 잡아서 내용을 출력해주는 이유가 있을까요?

}

public void isValidDiscount(VoucherType type, long discount) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 클래스에서만 사용하는 메소드이니 private이 좋을것 같네요~

그보다 검증의 책임을 각 엔티티 클래스에서 수행하는게 어떨까요?
생성자 부분에서 검증하면 될것 같아요~

switch (type) {
case FIXED:
if (discount <= 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매직넘버 잊지 말아주세요~ 적절한 변수명을 가진 상수로 빼주기!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상수로 빼줘도 좋고, 전체 라인을 inlineMethod로 추출해도 좋고

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 내용과 관련해서.. 영주님 PR에서 멘토님께서 남겨주신 리뷰를 꼭 읽어보시면 좋을것 같습니다~
#769 (comment)

throw new IllegalArgumentException("고정 할인 바우처의 입력 금액은 0 이하를 입력할 수 없습니다.");
}
break;
case PERCENT:
if (discount < 1 || discount > 99) {
throw new IllegalArgumentException("퍼센트 할인 바우처의 할인 퍼센트는 1 ~ 99까지의 숫자를 입력해야 합니다.");
}
break;
}
}

public Map<UUID, Voucher> fetchAllVouchers() {
return voucherRepository.getAllVouchersList();
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/prgrms/springbootbasic/view/Console.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgrms.springbootbasic.view;

import com.prgrms.springbootbasic.domain.Voucher;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import java.util.Map;
import java.util.Scanner;
import java.util.UUID;
Expand All @@ -26,13 +26,14 @@ public String inputCommand() {

@Override
public String inputVoucherType() {
System.out.print("생성할 voucher의 종류를 입력해주세요.(FixedDiscountVoucher, PercentDiscountVoucher) : ");
System.out.print("생성할 voucher의 종류를 입력해주세요.(fixed(고정 할인 바우처) , percent(퍼센트 할인 바우처) : ");
return input.nextLine();
}

@Override
public long inputVoucherDiscount() {
System.out.println("생성할 voucher의 금액을 입력해주세요!");
System.out.print("Discount : ");
return Long.parseLong(input.nextLine());
}

Expand All @@ -49,7 +50,7 @@ public void printlnVoucherList(Map<UUID, Voucher> voucherMap) {
}
System.out.println("생성된 Voucher의 목록은 다음과 같습니다.");
for (Voucher voucher : voucherMap.values()) {
System.out.println("Voucher Type: " + voucher.getVoucherType() + " + Discount" + voucher.getDiscount());
System.out.println("Voucher Type: " + voucher.getVoucherType() + ", 해당 Voucher의 Discount: " + voucher.getDiscount() + " 입니다.");
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/prgrms/springbootbasic/view/Output.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.prgrms.springbootbasic.view;

import com.prgrms.springbootbasic.domain.Voucher;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import java.util.Map;
import java.util.UUID;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.prgrms.springbootbasic.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.prgrms.springbootbasic.controller.voucher.VoucherController;
import com.prgrms.springbootbasic.domain.voucher.Voucher;
import com.prgrms.springbootbasic.enums.VoucherType;
import com.prgrms.springbootbasic.service.voucher.VoucherService;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

class VoucherControllerTest {

@Mock
private VoucherService voucherService;

private VoucherController voucherController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
voucherController = new VoucherController(voucherService);
}
Comment on lines +26 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

voucherController 위에 @InjectMocks 어노테이션을 사용하면 따로 객체를 생성해주지 않아도 됩니다~


@Test
@DisplayName("바우처 목록 validation 테스트")
void getVoucherListControllerTest() {
// given
Map<UUID, Voucher> voucherMap = Collections.emptyMap();
when(voucherService.fetchAllVouchers()).thenReturn(voucherMap);

// when
Map<UUID, Voucher> voucherList = voucherController.printVoucherList();

// then
assertThat(voucherList).isEmpty();
verify(voucherService, times(1)).fetchAllVouchers();
}

@Test
@DisplayName("바우처 생성 validation 테스트")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다른곳 다 동일합니다. 뭘 테스트 하려는지 기대값,목적이 여기선 보이지 않네요.

void getVoucherCreateControllerTest() {
// given
long discount = 10000;

// when
voucherController.createVoucher(VoucherType.FIXED, discount);

// then
verify(voucherService, times(1)).createVoucher(VoucherType.FIXED, discount);
}
}


Loading