Skip to content

Commit

Permalink
๐Ÿ”ง Modify : ํŒŒ์ผ/ํด๋” ์ˆ˜์ •/์‚ญ์ œ/์œ„์น˜ ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
๐Ÿค– Refactor : ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง
โœ… Test : ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€

1. ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด์„  ๋‚œ์ˆ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” Random ๊ฐ์ฒด๋ฅผ mockํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
2. MoneyToPurchaseLotto ๋‚ด๋ถ€์—์„œ Random์„ ์ƒ์„ฑํ•˜๋ฉด mock ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•  ์ˆ˜ ์—†๋‹ค.
3. args์ฒ˜๋Ÿผ randome ๊ฐ์ฒด๋ฅผ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ๊ทธ๋žจ์„ Game์œผ๋กœ ํ•œ๋ฒˆ ๋” ๊ฐ์ŒŒ๋‹ค.
4. testํ•  ๋•Œ mock random ๊ฐ์ฒด๋ฅผ Game์— ์ฃผ์ž…ํ•ด์„œ ํ†ตํ•ฉํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Loading branch information
haxr369 committed Mar 21, 2024
1 parent ce85e62 commit d58e633
Show file tree
Hide file tree
Showing 7 changed files with 1,611 additions and 84 deletions.
1,481 changes: 1,481 additions & 0 deletions hs_err_pid45149.log

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/base/view/ConsoleInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ private void printInputHeader() {
}

public void close() {
scanner.close();
// scanner.close();
}
}
52 changes: 3 additions & 49 deletions src/main/java/lotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,10 @@
package lotto;

import java.io.ByteArrayInputStream;
import java.util.Scanner;

import lotto.controller.MoneyConsoleInput;
import lotto.controller.MoneyToPurchaseLotto;
import lotto.controller.PurchaseLottoToPrize;
import lotto.controller.WinningBonusConsoleInput;
import lotto.controller.WinningLottoConsoleInput;
import lotto.model.bonus.BonusOutputDto;
import lotto.model.bonus.IBonusOutput;
import lotto.model.lotto.winning.IWinningLottoOutput;
import lotto.model.money.IMoneyInput;
import java.util.Random;

public class Application {

public static void main(String[] args) {
if (args.length < 3) {
args = new String[]{"8000", "1,2,3,4,5,6", "7"}; // ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๊ฐ€์ •ํ•˜๋Š” ๊ฑธ๋กœ ์˜ˆ์ƒ
}
ByteArrayInputStream actual = new ByteArrayInputStream(
String.join(System.lineSeparator(), args).getBytes());
System.setIn(actual);
Scanner scanner = new Scanner(System.in);
// ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ
IMoneyInput money;
try (var console = MoneyConsoleInput.of(scanner)) {
money = console.getInput();
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋กœ๋˜ ๊ตฌ๋งค
var purchasedLotto = MoneyToPurchaseLotto.of().tryConvert(money);

// ๋‹น์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ
IWinningLottoOutput winningLotto;
try (var console = WinningLottoConsoleInput.of(scanner)) {
winningLotto = IWinningLottoOutput.of(console.getInput());
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ
IBonusOutput bonusNumber;
try (var console = WinningBonusConsoleInput.of(winningLotto, scanner)) {
bonusNumber = BonusOutputDto.of(console.getInput());
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋‹น์ฒจ ํ†ต๊ณ„ ์ถœ๋ ฅ
PurchaseLottoToPrize.of(winningLotto, bonusNumber).tryConvert(purchasedLotto);
Game game = new Game(args, new Random());
game.runGame();
}
}
60 changes: 60 additions & 0 deletions src/main/java/lotto/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package lotto;

import lotto.controller.*;
import lotto.model.bonus.BonusOutputDto;
import lotto.model.bonus.IBonusOutput;
import lotto.model.lotto.winning.IWinningLottoOutput;
import lotto.model.money.IMoneyInput;

import java.io.ByteArrayInputStream;
import java.util.Random;
import java.util.Scanner;

public class Game {
private final String[] args;
private final Random random;
public Game(String[] args, Random random) {
this.args = args;
this.random = random;
}

public void runGame(){

if (args.length >= 3) {
ByteArrayInputStream actual = new ByteArrayInputStream(
String.join(System.lineSeparator(), args).getBytes());
System.setIn(actual);
}

Scanner scanner = new Scanner(System.in);
// ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ
IMoneyInput money;
try (var console = MoneyConsoleInput.of(scanner)) {
money = console.getInput();
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋กœ๋˜ ๊ตฌ๋งค
var purchasedLotto = MoneyToPurchaseLotto.of(random).tryConvert(money);

// ๋‹น์ฒจ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ
IWinningLottoOutput winningLotto;
try (var console = WinningLottoConsoleInput.of(scanner)) {
winningLotto = IWinningLottoOutput.of(console.getInput());
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ
IBonusOutput bonusNumber;
try (var console = WinningBonusConsoleInput.of(winningLotto, scanner)) {
bonusNumber = BonusOutputDto.of(console.getInput());
} catch (Exception e) {
throw new RuntimeException(e);
}

// ๋‹น์ฒจ ํ†ต๊ณ„ ์ถœ๋ ฅ
PurchaseLottoToPrize.of(winningLotto, bonusNumber).tryConvert(purchasedLotto);
}
}
11 changes: 6 additions & 5 deletions src/main/java/lotto/controller/MoneyToPurchaseLotto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

public class MoneyToPurchaseLotto extends Converter<IMoneyInput, ILottoPurchaseOutput> {

public MoneyToPurchaseLotto(View<ILottoPurchaseOutput> view) {
private final Random random;
private MoneyToPurchaseLotto(View<ILottoPurchaseOutput> view, Random random) {
super(view);
this.random = random;
}

public static MoneyToPurchaseLotto of() {
public static MoneyToPurchaseLotto of(Random random) {
return new MoneyToPurchaseLotto(
new LottoPurchaseOutputView()
);
new LottoPurchaseOutputView(),
random);
}

@Override
Expand All @@ -35,7 +37,6 @@ public ILottoPurchaseOutput convert(IMoneyInput iMoneyInput) {
}

private LottoOutputDto generateRandomLotto() {
var random = new Random();
var numbers = new TreeSet<Integer>();
while (numbers.size() < 6) {
numbers.add(random.nextInt(45) + 1);
Expand Down
63 changes: 42 additions & 21 deletions src/test/java/lotto/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Random;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.mockito.Mockito;
import org.mockito.stubbing.OngoingStubbing;
import static org.mockito.Mockito.when;

public class ApplicationTest {
private final String lf = System.lineSeparator();
Expand All @@ -24,34 +29,50 @@ void tearDown() {
System.setOut(System.out);
}

@Disabled
// @Disabled
@DisplayName("Application ์‹คํ–‰ ํ…Œ์ŠคํŠธ")
@Test
void integrationTest() {
// given
// ๋ฌด์ž‘์œ„๋กœ ์ƒ์„ฑ๋˜๋Š” Random ๋ณ€์ˆ˜๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์—†์„๊นŒ?
String[] args = {"8000", "1,2,3,4,5,6", "7"};
String expected = makeStringByArgs(args);
Random randomNumberMock = Mockito.mock(Random.class);
int [][] randomValues = new int[][]{
{1, 2, 3, 41, 42, 43},
{1, 2, 9, 41, 42, 43},
{3, 5, 11, 16, 32, 38},
{7, 11, 16, 35, 36, 44},
{13, 14, 16, 38, 42, 44},
{2, 13, 22, 32, 38, 41},
{2, 13, 22, 32, 38, 43},
{2, 13, 22, 32, 38, 42}
};
String expected = makeStringByArgs(args, randomValues);
OngoingStubbing<Integer> stubbing = when(randomNumberMock.nextInt(45));
for (int[] values : randomValues) {
for (int value : values) {
stubbing = stubbing.thenReturn(value);
}
}

// when
Application.main(args);
Game game = new Game(args, randomNumberMock);
game.runGame();
// then
Assertions.assertEquals(expected.trim(), actual.toString().trim());
assertEquals(expected.trim(), actual.toString().trim());
}

private String makeStringByArgs(String[] args) {
String inputMoney = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf + args[0] + lf;
String outputPurchasedLotto = """
8๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.
[8, 21, 23, 41, 42, 43]
[3, 5, 11, 16, 32, 38]
[7, 11, 16, 35, 36, 44]
[1, 8, 11, 31, 41, 42]
[13, 14, 16, 38, 42, 45]
[7, 11, 30, 40, 42, 43]
[2, 13, 22, 32, 38, 45]
[1, 3, 5, 14, 22, 45]
""".replace("\n", lf);
String inputWinningLotto = "๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf + args[1] + lf;
String inputWinningBonus = "๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf + args[2] + lf;
private String makeStringByArgs(String[] args, int[][] randomValues) {
String inputMoney = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf ; //+ args[0] + lf;
StringBuilder sb = new StringBuilder();
sb.append(String.format("%d๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค.\n",8));
for(int i=0; i<randomValues.length; i++){
sb.append(Arrays.toString(Arrays.stream(randomValues[i]).map(n->n+1).toArray()));
sb.append("\n");
}
String outputPurchasedLotto = sb.toString();
String inputWinningLotto = "๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf; // + args[1] + lf;
String inputWinningBonus = "๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”." + lf; // + args[2] + lf;
String outputPrizeStatistics = """
๋‹น์ฒจ ํ†ต๊ณ„
---
Expand Down
26 changes: 18 additions & 8 deletions src/test/java/lotto/controller/LottoPurchaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import java.io.ByteArrayInputStream;
import java.util.Scanner;

import lotto.model.money.MoneyInputValidator;
import lotto.view.sin.MoneyInputView;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -19,11 +15,11 @@ private MoneyConsoleInput getController(Scanner scanner) {
return MoneyConsoleInput.of(scanner);
}

@Disabled
@DisplayName("์Œ์ด ์•„๋‹Œ Integer์— ๋Œ€ํ•œ ๋ˆ ๋ฐœํ–‰")
// @Disabled
@DisplayName("1000์— ๋‚˜๋ˆ  ๋–จ์–ด์ง€๋Š” Integer์— ๋Œ€ํ•œ ๋ˆ ๋ฐœํ–‰")
@ParameterizedTest
@ValueSource(ints = {0, 1, 1000, Integer.MAX_VALUE})
void inputMoney(int number) {
@ValueSource(ints = {1000,2000,3000})
void inputDontHaveRemainMoney(int number) {
// given
String input = number + lf;
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
Expand All @@ -34,6 +30,20 @@ void inputMoney(int number) {
// then
Assertions.assertDoesNotThrow(controller::tryInput);
}
@DisplayName("1000์— ๋‚˜๋ˆ  ๋–จ์–ด์ง€์ง€ ์•Š๋Š” Integer์— ๋Œ€ํ•œ ๋ˆ ๋ฐœํ–‰")
@ParameterizedTest
@ValueSource(ints = {0, 1, Integer.MAX_VALUE})
void inputHaveRemainMoney(int number) {
// given
String input = number + lf;
ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
System.setIn(new ByteArrayInputStream(input.getBytes()));
var controller = getController(new Scanner(System.in));

// then
Assertions.assertThrows(IllegalArgumentException.class,controller::tryInput);
}

@DisplayName("์Œ์ˆ˜๋กœ๋Š” ๋ˆ ๋ฐœํ–‰ ๋ถˆ๊ฐ€")
@ParameterizedTest
Expand Down

0 comments on commit d58e633

Please sign in to comment.