Skip to content

Commit

Permalink
결과값 출력 메서드 테스트코드 추가 (고려해야할점 : 프린트문으로 결과값을 출력하는 경우 반환값이 없어 검증이 어려움, 이때…
Browse files Browse the repository at this point in the history
…는 테스트코드 어떻게?)
  • Loading branch information
silano08 committed Apr 8, 2024
1 parent e2b5df3 commit b27cbc9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/baseball/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public static String checkNumberBall(String input){
}
}

private static void printResult(int strike,int ball,boolean isCorrect){
public static void printResult(int strike,int ball,boolean isCorrect){
// 결과값을 출력(종료인지 아닌지)
if((strike + ball) > 3) throw new RuntimeException("잘못된 요청입니다.");

if(isCorrect){
System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료");
}else if(strike>0 && ball>0){
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/baseball/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void runMain() {
"'1234', false, '숫자가 너무 깁니다.'", // 긴 입력
"'abc', false, '정수가 아닌 입력입니다. 다시 시도해주세요.'", // 비숫자 입력
"'', false, '숫자를 입력해주세요'", // 빈 문자열 입력
"null, false, '숫자를 입력해주세요'" // null 입력
})
@DisplayName("checkNumberBall 메서드의 경곗값 테스트")
void testCheckNumberBall(String input, boolean isValid, String expectedMessage) {
Expand All @@ -61,4 +62,20 @@ void testCheckNumberBall(String input, boolean isValid, String expectedMessage)
Assertions.assertEquals(expectedMessage, exception.getMessage());
}
}

@Test
void testPrintResultWithInvalidInput() {
// 프린트문으로 결과값을 출력하는 경우 반환값이 없어 검증이 어려움...
Exception exception = assertThrows(RuntimeException.class, () -> {
Application.printResult(2, 2, false); // 스트라이크와 볼의 합이 3을 초과
});
assertEquals("잘못된 요청입니다.", exception.getMessage());

// 사실 아래와같은 경우를 검증해야함
// 스트라이크와 볼의 합이 3을 초과하는 경우 (예외 발생)
// 정확히 3개의 숫자를 맞혔을 때 (게임 종료 메시지 출력)
// 스트라이크와 볼이 모두 있는 경우
// 오직 볼만 있는 경우
// 오직 스트라이크만 있는 경우
}
}

0 comments on commit b27cbc9

Please sign in to comment.