Java 使用junit测试输入
所以我开发了这个非常简单的Tictaoe游戏。我想测试下面给出的方法:Java 使用junit测试输入,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,所以我开发了这个非常简单的Tictaoe游戏。我想测试下面给出的方法: public class Board { private Scanner scan = new Scanner(System.in); public int inputBoardSize() { while (flag) { System.out.print("Enter the number of grids you want to play with:");
public class Board {
private Scanner scan = new Scanner(System.in);
public int inputBoardSize() {
while (flag) {
System.out.print("Enter the number of grids you want to play with:");
try {
boardSize = Integer.parseInt(scan.next());
if (boardSize < 3 || boardSize > 10) {
System.out.println("Please choose a board size between 3 and 10");
continue;
}
flag = false;
break;
} catch (NumberFormatException e) {
e.getMessage();
System.out.println("Please enter a number");
continue;
}
}
printBoard(boardSize);
return boardSize;
}
我无法确定下一步要做什么。添加以下构造函数:
public class Board {
private Scanner;
public Board() {
this(System.in);
}
public Board(InputStream in) {
scan = new Scanner(in);
}
}
然后,您可以在测试类时从外部提供一个
InputStream
。您可以将验证逻辑提取到单独的方法中,然后测试该方法。这样就不需要交互或注入扫描仪
对象。提取的代码类似于
public int inputBoardSize() {
while (flag) {
System.out.print("Enter the number of grids you want to play with:");
validateBoardSize(scan.next());
}
printBoard(boardSize);
return boardSize;
}
protected void validateBoardSize(String input) {
try {
boardSize = Integer.parseInt(input);
if (boardSize < 3 || boardSize > 10) {
System.out.println("Please choose a board size between 3 and 10");
}
else {
flag = false;
}
}
catch (NumberFormatException e) {
e.getMessage();
System.out.println("Please enter a number");
}
}
请注意,由于您的程序通过通过标准输出发送的消息表示错误(与引发异常相反),因此测试必须截获发送到标准输出的输出消息,并进行字符串比较,以查看消息的内容。因此,在setUp()
中,标准输出的OutputStream
被设置为一个OutputStream
实例,其字符串值可以通过以下测试方法进行比较:System.setOut(新的打印流(outContent))
。要测试输出,只需提取OutputStream
:outContent.toString().trim()的字符串值即可。请注意,trim()
用于删除尾随的换行符,因为println
将在错误消息中包含它们。换行符是操作系统敏感的,因此删除它们会使比较字符串更加简单
有关更多信息,请参阅。如何测试第三个条件:输入为3和10?您需要使用所谓的。为方法提供Scanner实例。在生产环境中,您可以使用系统。在测试环境中,您可以使用不同的InputStream
来播放您的测试用例。我确实使用了InputStream。类似这样的内容:@Test public void Test(){Board Board=new Board();String input=“2”;InputStream in=new ByteArrayInputStream(input.getBytes());System.setIn(in);}那么是什么阻止您创建将“2”
更改为“11”的此测试方法的副本呢
或“不是数字”
?非常感谢!这对理解它的工作原理有很大帮助。因此我使用System.err而不是System.out。并且单元测试失败org.junit.ComparisonFailure:应为:但为:。是否有其他方法来测试system.err语句?有一种方法可以测试标准错误而不是标准输出。在setUp()
方法中,将System.setOut(新打印流(outContent))更改为System.setErr(新打印流(outContent))
。这将允许您捕获并测试打印到标准错误的输出。有关其工作原理的更多信息,请参阅。
public int inputBoardSize() {
while (flag) {
System.out.print("Enter the number of grids you want to play with:");
validateBoardSize(scan.next());
}
printBoard(boardSize);
return boardSize;
}
protected void validateBoardSize(String input) {
try {
boardSize = Integer.parseInt(input);
if (boardSize < 3 || boardSize > 10) {
System.out.println("Please choose a board size between 3 and 10");
}
else {
flag = false;
}
}
catch (NumberFormatException e) {
e.getMessage();
System.out.println("Please enter a number");
}
}
public class BoardTest {
private static final String OUT_OUT_BOUNDS_ERROR_MESSAGE = "Please choose a board size between 3 and 10";
private static final String NFE_ERROR_MESSAGE = "Please enter a number";
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private Board board;
@Before
public void setUp() {
System.setOut(new PrintStream(outContent));
board = new Board();
}
@Test
public void setBoardSizeOf2EnsureErrorMessageDisplayed() {
board.validateBoardSize("2");
assertOutOfBoundsErrorMessageDisplayed();
}
private void assertOutOfBoundsErrorMessageDisplayed() {
assertEquals(OUT_OUT_BOUNDS_ERROR_MESSAGE, outContent.toString().trim());
}
@Test
public void setBoardSizeOf3EnsureNoErrorMessageDisplayed() {
board.validateBoardSize("3");
assertNoErrorMessageDisplayed();
}
private void assertNoErrorMessageDisplayed() {
assertEquals("", outContent.toString().trim());
}
@Test
public void setBoardSizeOf4EnsureNoErrorMessageDisplayed() {
board.validateBoardSize("4");
assertNoErrorMessageDisplayed();
}
@Test
public void setBoardSizeOf9EnsureNoErrorMessageDisplayed() {
board.validateBoardSize("9");
assertNoErrorMessageDisplayed();
}
@Test
public void setBoardSizeOf10EnsureNoErrorMessageDisplayed() {
board.validateBoardSize("10");
assertNoErrorMessageDisplayed();
}
@Test
public void setBoardSizeOf11EnsureErrorMessageDisplayed() {
board.validateBoardSize("11");
assertOutOfBoundsErrorMessageDisplayed();
}
@Test
public void setBoardSizeWithInvalidNumberEnsureInvalidNumberMessageDisplayed() {
board.validateBoardSize("blah");
assertInvalidNumberMessageDisplayed();
}
private void assertInvalidNumberMessageDisplayed() {
assertEquals(NFE_ERROR_MESSAGE, outContent.toString().trim());
}
}