-
Notifications
You must be signed in to change notification settings - Fork 171
[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
base: jay-so
Are you sure you want to change the base?
Changes from all commits
5e0f655
5f8b98f
02dfa0f
f1f9c5f
10b60ad
ce917df
2ba1de8
5c158a1
0d6236e
50faf9e
b244133
3561696
65775ed
7b6afd7
fac02cf
85a7f28
b4d1d4d
b253bc7
64686ea
eff8ead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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()); | ||
} | ||
} | ||
} | ||
|
||
private void createVoucher() { | ||
public void createVoucher() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("바우처가 생성되었습니다!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
This file was deleted.
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. root 레벨에서 해당 예외를 catch하고 있는데 중간에 한번 더 잡아서 내용을 출력해주는 이유가 있을까요? |
||
} | ||
|
||
public void isValidDiscount(VoucherType type, long discount) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 클래스에서만 사용하는 메소드이니 private이 좋을것 같네요~ 그보다 검증의 책임을 각 엔티티 클래스에서 수행하는게 어떨까요? |
||
switch (type) { | ||
case FIXED: | ||
if (discount <= 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매직넘버 잊지 말아주세요~ 적절한 변수명을 가진 상수로 빼주기! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수로 빼줘도 좋고, 전체 라인을 inlineMethod로 추출해도 좋고 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 내용과 관련해서.. 영주님 PR에서 멘토님께서 남겨주신 리뷰를 꼭 읽어보시면 좋을것 같습니다~ |
||
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(); | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. voucherController 위에 |
||
|
||
@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 테스트") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stackTrace가 있다면 나중에 로그를 볼때 좀더 빠르게 파악할것 같아요.