From b05cbc0c15111956a36f38959be932dea4aa2006 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 16:18:52 +0900 Subject: [PATCH 01/14] feat: first commit --- src/main/java/baseball/Application.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/baseball/Application.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java new file mode 100644 index 00000000..cd55e1ed --- /dev/null +++ b/src/main/java/baseball/Application.java @@ -0,0 +1,7 @@ +package baseball; + +public class Application { + public static void main(String[] args) { + System.out.println("Hello 숫자 야구"); + } +} \ No newline at end of file From 3d4276447f2f2b1da76a6a59bc30acc9aec53118 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 16:36:29 +0900 Subject: [PATCH 02/14] docs: Update README.md --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d7e8aee..6d70197a 100644 --- a/README.md +++ b/README.md @@ -1 +1,26 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse + +## 프로젝트 소개 +컴퓨터가 1에서 9까지의 서로 다른 임의의 수 3개를 선택하면, 사용자가 그 숫자를 맞추는 게임입니다. + +입력한 숫자에 대한 결과(볼, 스트라이크, 낫싱)를 힌트로 얻어 컴퓨터의 수를 모두 맞추면 승리합니다. + +## 기능 목록 + +- **랜덤 숫자 생성**: 1에서 9까지 서로 다른 임의의 수 3개를 선택하여 저장한다. + + +- **재시작/종료 입력**: 게임 종료 후 재시작(1) 또는 종료(2)를 구분하는 숫자를 입력받는다. + + +- **입력값 예외 처리**: 사용자가 잘못된 값을 입력했는지 검증한다. + - 숫자가 아닌 값이 포함된 경우 + - 3자리가 아닌 경우 + - 1~9 사이의 숫자가 아닌 경우 (0 포함 시) + - 중복된 숫자가 있는 경우 + + +- **판정 로직**: 컴퓨터의 수와 사용자의 수를 비교하여 결과를 계산한다. + - 같은 수가 같은 자리에 있으면 **스트라이크** + - 같은 수가 다른 자리에 있으면 **볼** + - 같은 수가 전혀 없으면 **낫싱** From 9aac7d4c3356f6502596266ec9773879d00280b1 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 16:43:18 +0900 Subject: [PATCH 03/14] feat: implement Random number generator --- src/main/java/baseball/Application.java | 8 ++++++++ src/main/java/baseball/Computer.java | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/main/java/baseball/Computer.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index cd55e1ed..85e1b5b4 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,7 +1,15 @@ package baseball; +import java.util.ArrayList; +import java.util.List; + public class Application { public static void main(String[] args) { System.out.println("Hello 숫자 야구"); + + Computer computer = new Computer(); + List numbers = computer.generateNumbers(); + + System.out.println("Computer's number: " + numbers); } } \ No newline at end of file diff --git a/src/main/java/baseball/Computer.java b/src/main/java/baseball/Computer.java new file mode 100644 index 00000000..4696bc95 --- /dev/null +++ b/src/main/java/baseball/Computer.java @@ -0,0 +1,22 @@ +package baseball; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Computer { + public List generateNumbers() { + List computer = new ArrayList<>(); + Random random = new Random(); + + while (computer.size() < 3) { + int randomNumber = random.nextInt(9) + 1; + + if (!computer.contains(randomNumber)) { + computer.add(randomNumber); + } + } + return computer; + } + +} From 32f10d090fb1d2ddec62d7b1bed3af5d599608b8 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 16:46:57 +0900 Subject: [PATCH 04/14] docs: update userinput function in README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6d70197a..2dadbf2b 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ - **재시작/종료 입력**: 게임 종료 후 재시작(1) 또는 종료(2)를 구분하는 숫자를 입력받는다. +- **사용자 숫자 입력**: 사용자가 예측한 숫자 값을 입력 + - **입력값 예외 처리**: 사용자가 잘못된 값을 입력했는지 검증한다. - 숫자가 아닌 값이 포함된 경우 From 6a730042a3e8aacf8841b39cbd03c8d90136c2be Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 16:50:56 +0900 Subject: [PATCH 05/14] feat: implement user input --- src/main/java/baseball/Application.java | 5 +++++ src/main/java/baseball/InputView.java | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/baseball/InputView.java diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 85e1b5b4..f303cd78 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -11,5 +11,10 @@ public static void main(String[] args) { List numbers = computer.generateNumbers(); System.out.println("Computer's number: " + numbers); + + InputView inputView = new InputView(); + String userInput = inputView.inputUserNumber(); + + System.out.println("User input: " + userInput); } } \ No newline at end of file diff --git a/src/main/java/baseball/InputView.java b/src/main/java/baseball/InputView.java new file mode 100644 index 00000000..6e81b89d --- /dev/null +++ b/src/main/java/baseball/InputView.java @@ -0,0 +1,21 @@ +package baseball; + +import java.util.Scanner; + +public class InputView { + private final Scanner scanner; + + public InputView() { + this.scanner = new Scanner(System.in); + } + + public String inputUserNumber() { + System.out.print("숫자를 입력해 주세요 : "); + return scanner.nextLine(); // 사용자가 친 내용을 문자열로 다 가져옴 + } + + public String inputRestartCommand() { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + return scanner.nextLine(); + } +} \ No newline at end of file From 1ec23138adb792df041f7ae12ad5b1d084091e85 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:07:46 +0900 Subject: [PATCH 06/14] feat: implement validation function --- src/main/java/baseball/InputValidator.java | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/baseball/InputValidator.java diff --git a/src/main/java/baseball/InputValidator.java b/src/main/java/baseball/InputValidator.java new file mode 100644 index 00000000..08435569 --- /dev/null +++ b/src/main/java/baseball/InputValidator.java @@ -0,0 +1,43 @@ +package baseball; + +import java.util.HashSet; +import java.util.Set; + +public class InputValidator { + + public void validate(String input) { + validateLength(input); + validateNumeric(input); + validateRange(input); + validateUnique(input); + } + + private void validateLength(String input) { + if (input.length() != 3) { + throw new IllegalArgumentException("[ERROR] 3자리의 숫자를 입력해야 합니다."); + } + } + + private void validateNumeric(String input) { + if (!input.matches("\\d+")) { + throw new IllegalArgumentException("[ERROR] 숫자만 입력해야 합니다."); + } + } + + private void validateRange(String input) { + if (input.contains("0")) { + throw new IllegalArgumentException("[ERROR] 1에서 9 사이의 숫자여야 합니다."); + } + } + + private void validateUnique(String input) { + Set uniqueChars = new HashSet<>(); + for (char c : input.toCharArray()) { + uniqueChars.add(c); + } + + if (uniqueChars.size() != 3) { + throw new IllegalArgumentException("[ERROR] 서로 다른 숫자를 입력해야 합니다."); + } + } +} \ No newline at end of file From 6de99ae1c4ad86e334de5961e506e863ec663c8c Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:08:16 +0900 Subject: [PATCH 07/14] refactor: update Application --- src/main/java/baseball/Application.java | 34 ++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index f303cd78..09be743a 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,20 +1,42 @@ package baseball; -import java.util.ArrayList; import java.util.List; public class Application { + private final Computer computer; + private final InputView inputView; + private final InputValidator validator; + + public Application() { + this.computer = new Computer(); + this.inputView = new InputView(); + this.validator = new InputValidator(); + } + public static void main(String[] args) { + new Application().run(); + } + + public void run() { System.out.println("Hello 숫자 야구"); - Computer computer = new Computer(); List numbers = computer.generateNumbers(); - System.out.println("Computer's number: " + numbers); - - InputView inputView = new InputView(); - String userInput = inputView.inputUserNumber(); + String userInput = getValidInput(); System.out.println("User input: " + userInput); + + } + + private String getValidInput() { + while (true) { + try { + String input = inputView.inputUserNumber(); + validator.validate(input); + return input; + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } } } \ No newline at end of file From c9613ca8922bbe6c665b43490079afe1e2f2a832 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:17:03 +0900 Subject: [PATCH 08/14] feat: implement user number judge function --- src/main/java/baseball/Judge.java | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/baseball/Judge.java diff --git a/src/main/java/baseball/Judge.java b/src/main/java/baseball/Judge.java new file mode 100644 index 00000000..484bd160 --- /dev/null +++ b/src/main/java/baseball/Judge.java @@ -0,0 +1,48 @@ +package baseball; + +import java.util.List; + +public class Judge { + + public String compare(List computer, List user) { + int strikes = countStrikes(computer, user); + int balls = countBalls(computer, user); + + if (strikes == 0 && balls == 0) { + return "낫싱"; + } + + return printResult(balls, strikes); + } + + private int countStrikes(List computer, List user) { + int strikes = 0; + for (int i = 0; i < computer.size(); i++) { + if (computer.get(i).equals(user.get(i))) { + strikes++; + } + } + return strikes; + } + + private int countBalls(List computer, List user) { + int balls = 0; + for (int i = 0; i < computer.size(); i++) { + if (!computer.get(i).equals(user.get(i)) && computer.contains(user.get(i))) { + balls++; + } + } + return balls; + } + + private String printResult(int balls, int strikes) { + StringBuilder sb = new StringBuilder(); + if (balls > 0) { + sb.append(balls).append("볼 "); + } + if (strikes > 0) { + sb.append(strikes).append("스트라이크"); + } + return sb.toString().trim(); + } +} \ No newline at end of file From c467575324f27ee59c5af3f0fdf3635852fc4ba3 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:18:01 +0900 Subject: [PATCH 09/14] refactor: update Application --- src/main/java/baseball/Application.java | 28 ++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 09be743a..b9cf4aa0 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -1,16 +1,19 @@ package baseball; +import java.util.ArrayList; import java.util.List; public class Application { private final Computer computer; private final InputView inputView; private final InputValidator validator; + private final Judge judge; public Application() { this.computer = new Computer(); this.inputView = new InputView(); this.validator = new InputValidator(); + this.judge = new Judge(); } public static void main(String[] args) { @@ -19,13 +22,32 @@ public static void main(String[] args) { public void run() { System.out.println("Hello 숫자 야구"); + List computerNumbers = computer.generateNumbers(); - List numbers = computer.generateNumbers(); + System.out.println("정답: " + computerNumbers); - String userInput = getValidInput(); + while (true) { + String userInputStr = getValidInput(); + + List userNumbers = convertToList(userInputStr); + + String result = judge.compare(computerNumbers, userNumbers); + System.out.println(result); + + if (result.equals("3스트라이크")) { + System.out.println("3개의 숫자를 모두 맞혔습니다!"); + break; + } + } - System.out.println("User input: " + userInput); + } + private List convertToList(String input) { + List numbers = new ArrayList<>(); + for (char c : input.toCharArray()) { + numbers.add(Character.getNumericValue(c)); + } + return numbers; } private String getValidInput() { From fdb50863397a200786cb8b34079343fc5a7eb4f8 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:30:14 +0900 Subject: [PATCH 10/14] feat: implement repeat function --- src/main/java/baseball/InputValidator.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/baseball/InputValidator.java b/src/main/java/baseball/InputValidator.java index 08435569..ebd0d542 100644 --- a/src/main/java/baseball/InputValidator.java +++ b/src/main/java/baseball/InputValidator.java @@ -40,4 +40,10 @@ private void validateUnique(String input) { throw new IllegalArgumentException("[ERROR] 서로 다른 숫자를 입력해야 합니다."); } } + + public void validateRestart(String input) { + if (!input.equals("1") && !input.equals("2")) { + throw new IllegalArgumentException("[ERROR] 1 또는 2를 입력해야 합니다."); + } + } } \ No newline at end of file From 6663b032bd52c15b66748057f71c5eb21943036c Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:30:23 +0900 Subject: [PATCH 11/14] refactor: update Application --- src/main/java/baseball/Application.java | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index b9cf4aa0..493fbf08 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -21,11 +21,28 @@ public static void main(String[] args) { } public void run() { - System.out.println("Hello 숫자 야구"); - List computerNumbers = computer.generateNumbers(); + System.out.println("숫자 야구 게임을 시작합니다."); + do { + newGame(); + } while (isRestart()); + System.out.println("숫자 야구 게임을 종료합니다."); + } - System.out.println("정답: " + computerNumbers); + private boolean isRestart() { + while (true) { + try { + String input = inputView.inputRestartCommand(); + validator.validateRestart(input); + return input.equals("1"); + } catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + } + } + public void newGame() { + List computerNumbers = computer.generateNumbers(); + System.out.println("정답(테스트용): " + computerNumbers); while (true) { String userInputStr = getValidInput(); From b0d155ffa3033c3aae74e9a892314c8604ce1af8 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 17:40:40 +0900 Subject: [PATCH 12/14] style: minor fix --- src/main/java/baseball/Application.java | 7 ++----- src/main/java/baseball/InputValidator.java | 2 +- src/main/java/baseball/InputView.java | 2 +- src/main/java/baseball/Judge.java | 1 - 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/baseball/Application.java b/src/main/java/baseball/Application.java index 493fbf08..c07f4896 100644 --- a/src/main/java/baseball/Application.java +++ b/src/main/java/baseball/Application.java @@ -42,21 +42,18 @@ private boolean isRestart() { public void newGame() { List computerNumbers = computer.generateNumbers(); - System.out.println("정답(테스트용): " + computerNumbers); while (true) { String userInputStr = getValidInput(); - List userNumbers = convertToList(userInputStr); String result = judge.compare(computerNumbers, userNumbers); System.out.println(result); if (result.equals("3스트라이크")) { - System.out.println("3개의 숫자를 모두 맞혔습니다!"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 끝"); break; } } - } private List convertToList(String input) { @@ -78,4 +75,4 @@ private String getValidInput() { } } } -} \ No newline at end of file +} diff --git a/src/main/java/baseball/InputValidator.java b/src/main/java/baseball/InputValidator.java index ebd0d542..c30e60f3 100644 --- a/src/main/java/baseball/InputValidator.java +++ b/src/main/java/baseball/InputValidator.java @@ -46,4 +46,4 @@ public void validateRestart(String input) { throw new IllegalArgumentException("[ERROR] 1 또는 2를 입력해야 합니다."); } } -} \ No newline at end of file +} diff --git a/src/main/java/baseball/InputView.java b/src/main/java/baseball/InputView.java index 6e81b89d..bab6b2f4 100644 --- a/src/main/java/baseball/InputView.java +++ b/src/main/java/baseball/InputView.java @@ -18,4 +18,4 @@ public String inputRestartCommand() { System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); return scanner.nextLine(); } -} \ No newline at end of file +} diff --git a/src/main/java/baseball/Judge.java b/src/main/java/baseball/Judge.java index 484bd160..e056056f 100644 --- a/src/main/java/baseball/Judge.java +++ b/src/main/java/baseball/Judge.java @@ -11,7 +11,6 @@ public String compare(List computer, List user) { if (strikes == 0 && balls == 0) { return "낫싱"; } - return printResult(balls, strikes); } From fac0a424e053412a674417094c420c8d9bf8c0c2 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Wed, 28 Jan 2026 18:37:01 +0900 Subject: [PATCH 13/14] docs: add test case in README.md --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 2dadbf2b..ea7cdd98 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ - **재시작/종료 입력**: 게임 종료 후 재시작(1) 또는 종료(2)를 구분하는 숫자를 입력받는다. + - **사용자 숫자 입력**: 사용자가 예측한 숫자 값을 입력 @@ -26,3 +27,37 @@ - 같은 수가 같은 자리에 있으면 **스트라이크** - 같은 수가 다른 자리에 있으면 **볼** - 같은 수가 전혀 없으면 **낫싱** + + +## 단위 테스트 + +- **랜덤 숫자 생성 테스트**: 3자리의 서로 다른 숫자가 올바르게 생성되어야 함. + - 생성된 숫자가 3자리인지 확인 + - 예: 12, 1234 + - 3개 숫자 중 동일한 숫자가 존재 + - 예: 112, 1233, 333 + - 생성된 숫자에 0 존재 + - 예: 012, 0123 + + +- **재시작/종료 입력 테스트**: 사용자는 1 또는 2를 입력해야 함. + - 1, 2를 제외한 다른 숫자를 입력 + - 예: 0, 12 + - 문자 입력 + - 예: kakao + + +- **사용자 숫자 입력 테스트**: 사용자는 서로 다른 3개의 숫자를 입력해야 함. + - 3자리가 아닌(2자리, 4자리 이상 등) 숫자 입력 + - 예: 12, 1234 + - 입력한 3개 숫자 중 동일한 숫자가 존재 + - 예: 112, 1233, 333 + - 입력한 숫자에 0 존재 + - 예: 012, 0123 + - 문자 입력 + - 예: kakao + + +- **판정 로직 테스트**: 사용자의 입력 값과 실제 값을 올바르게 판정해야 함. + - 여러가지 상황에서 스트라이크, 볼, 낫싱 비교 결과가 일치 (댜양한 case를 만들어보아야 함) + - 예: 428, 624, 921 등.. From 0f7722ce4ea7590693e995c8c0553c1836d1db77 Mon Sep 17 00:00:00 2001 From: gspear99 Date: Sun, 1 Feb 2026 15:34:50 +0900 Subject: [PATCH 14/14] test: add test code --- src/main/java/baseball/Judge.java | 2 +- src/test/java/baseball/ComputerTest.java | 29 +++++++ .../java/baseball/InputValidatorTest.java | 75 +++++++++++++++++++ src/test/java/baseball/JudgeTest.java | 56 ++++++++++++++ 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 src/test/java/baseball/ComputerTest.java create mode 100644 src/test/java/baseball/InputValidatorTest.java create mode 100644 src/test/java/baseball/JudgeTest.java diff --git a/src/main/java/baseball/Judge.java b/src/main/java/baseball/Judge.java index e056056f..320ccc7b 100644 --- a/src/main/java/baseball/Judge.java +++ b/src/main/java/baseball/Judge.java @@ -44,4 +44,4 @@ private String printResult(int balls, int strikes) { } return sb.toString().trim(); } -} \ No newline at end of file +} diff --git a/src/test/java/baseball/ComputerTest.java b/src/test/java/baseball/ComputerTest.java new file mode 100644 index 00000000..2c3f295a --- /dev/null +++ b/src/test/java/baseball/ComputerTest.java @@ -0,0 +1,29 @@ +package baseball; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + +class ComputerTest { + + @Test + @DisplayName("생성된 숫자는 3자리여야 하며, 1~9 사이의 중복 없는 수여야 한다") + void computerGenerateRandomCase() { + Computer computer = new Computer(); + + List numbers = computer.generateNumbers(); + + assertThat(numbers).hasSize(3); + + for (Integer number : numbers) { + assertThat(number).isBetween(1, 9); + } + + Set uniqueNumbers = new HashSet<>(numbers); + assertThat(uniqueNumbers).hasSize(3); + } +} diff --git a/src/test/java/baseball/InputValidatorTest.java b/src/test/java/baseball/InputValidatorTest.java new file mode 100644 index 00000000..524899bf --- /dev/null +++ b/src/test/java/baseball/InputValidatorTest.java @@ -0,0 +1,75 @@ +package baseball; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class InputValidatorTest { + + private final InputValidator validator = new InputValidator(); + + @Test + @DisplayName("정상 입력: 서로 다른 3자리 숫자는 통과해야 한다") + void inputNormal() { + assertThatCode(() -> validator.validate("123")) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"12", "1234"}) + @DisplayName("예외: 3자리가 아닌 숫자 입력 시 예외 발생") + void inputLongCase(String input) { + assertThatThrownBy(() -> validator.validate(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("[ERROR]"); + } + + @ParameterizedTest + @ValueSource(strings = {"a12", "kakao", "1 3"}) + @DisplayName("예외: 숫자가 아닌 문자 입력 시 예외 발생") + void inputStringCase(String input) { + assertThatThrownBy(() -> validator.validate(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("[ERROR]"); + } + + @ParameterizedTest + @ValueSource(strings = {"112", "121", "333"}) + @DisplayName("예외: 중복된 숫자가 있으면 예외 발생") + void inputDuplicateNum(String input) { + assertThatThrownBy(() -> validator.validate(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("[ERROR]"); + } + + @ParameterizedTest + @ValueSource(strings = {"012", "109"}) + @DisplayName("예외: 0이 포함된 숫자 입력 시 예외 발생") + void inputIncludeZeroCase(String input) { + assertThatThrownBy(() -> validator.validate(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("[ERROR]"); + } + + @Test + @DisplayName("재시작 정상: 1 또는 2 입력 시 통과") + void inputRedoCase() { + assertThatCode(() -> validator.validateRestart("1")) + .doesNotThrowAnyException(); + assertThatCode(() -> validator.validateRestart("2")) + .doesNotThrowAnyException(); + } + + @ParameterizedTest + @ValueSource(strings = {"0", "3", "a", "start"}) + @DisplayName("재시작 예외: 1, 2가 아닌 값 입력 시 예외 발생") + void inputWrongRedoCase(String input) { + assertThatThrownBy(() -> validator.validateRestart(input)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("[ERROR]"); + } +} diff --git a/src/test/java/baseball/JudgeTest.java b/src/test/java/baseball/JudgeTest.java new file mode 100644 index 00000000..49e5d4f9 --- /dev/null +++ b/src/test/java/baseball/JudgeTest.java @@ -0,0 +1,56 @@ +package baseball; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class JudgeTest { + + private final Judge judge = new Judge(); + + @Test + @DisplayName("3스트라이크: 위치와 숫자가 모두 같으면 3스트라이크 반환") + void judge3StrikeCase() { + List computer = List.of(1, 2, 3); + List user = List.of(1, 2, 3); + + String result = judge.compare(computer, user); + + assertThat(result).isEqualTo("3스트라이크"); + } + + @Test + @DisplayName("낫싱: 겹치는 숫자가 하나도 없으면 낫싱 반환") + void judgeNothingCase() { + List computer = List.of(1, 2, 3); + List user = List.of(4, 5, 6); + + String result = judge.compare(computer, user); + + assertThat(result).isEqualTo("낫싱"); + } + + @Test + @DisplayName("1볼 1스트라이크: 1개는 위치 같음, 1개는 위치 다름") + void judge1Ball1StrikeCase() { + List computer = List.of(1, 2, 3); + List user = List.of(1, 3, 4); + + String result = judge.compare(computer, user); + + assertThat(result).isEqualTo("1볼 1스트라이크"); + } + + @Test + @DisplayName("2볼: 2개의 숫자가 값은 같으나 위치가 다름") + void judge2BallCase() { + List computer = List.of(1, 2, 3); + List user = List.of(2, 1, 5); + + String result = judge.compare(computer, user); + + assertThat(result).isEqualTo("2볼"); + } +}