@Test
void testRowX() {
// given
String sizeOfBoardString = "3\n2";
ByteArrayInputStream inputStream = new ByteArrayInputStream(sizeOfBoardString.getBytes());
System.setIn(new ByteArrayInputStream(sizeOfBoardString.getBytes()));
// when
OutputComputer outputComputer = new OutputComputer();
outputComputer.gameStart();
outputComputer.gameType();
InputSelector inputSelector = new InputSelector();
int boardSize = inputSelector.boardType();
StartGame startGame = new StartGame();
startGame.runGame(boardSize, boardSize);
String whoWin = "Won X";
// then
assertEquals("Wona X", whoWin);
}
我的 Java 测试有问题。到目前为止我知道如果我把 String sizeOfBoardString = "3 2”;我应该回答程序中的 2 个问题,但仍有一个问题需要回答。
总而言之,我正在尝试测试 tictactoe 程序。我询问了板子的尺寸,并得到了值。但当我问是计算机还是人类时,它告诉我没有价值。但在我看来,它应该是 2,因为该值: String sizeOfBoardString = "3 2";
如果我手动启动程序,它工作正常
public class StartGame {
public void runGame(int boardSize, int boardSize2) {
RealUser realUser = new RealUser();
Computer computer = new Computer();
OutputComputer outputComputer = new OutputComputer();
Board board = new Board(boardSize, boardSize);
outputComputer.computerOrHuman();
int compOrHum = board.computerOrHuman();
board.showFilledBoard();
}
还有一个叫
computerOrHuman()
public class Board {
private char[][] values;
private int rows;
private int columns;
List<Character> winnersX = new ArrayList<>();
List<Character> winnersO = new ArrayList<>();
public Board(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.values = new char[rows][columns];
}
public int computerOrHuman() {
InputSelector inputSelector = new InputSelector();
int ifHuman = inputSelector.computerOrHuman();
return ifHuman;
}
}
以及包含scanner.nextLine()的输入选择器
public class InputSelector {
Scanner scanner = new Scanner(System.in);
public int computerOrHuman() {
int computerOrHuman = Integer.parseInt(scanner.nextLine());
return computerOrHuman;
}
}
我应该做什么来编码工作?
这就是代码的结果: 欢迎来到井字游戏! 选择一种游戏类型。类型 3 或 10 你想和电脑玩(1)还是和人玩(2)?
未找到线路
public class OutputComputer {
public void computerOrHuman() {
System.out.println("Do you want to play with computer(1) or human(2)?");
}
}
如果断言不正确或缺少验证,某些测试框架(例如 JUnit)可能会提前将测试标记为通过或失败。 确保: 断言和验证放置正确。 测试第一个功能后测试并未结束。
您也可以分别测试这两个功能