理想情况下,我想编写JUnit测试代码,以交互方式测试学生基于文本的I / O应用程序。使用System.setIn()
/.setOut()
会导致问题,因为基础流正在阻塞。在较早的帖子(基于测试控制台的应用程序/程序-Java)中建议使用Birkner的系统规则(http://www.stefan-birkner.de/system-rules/index.html),但它似乎要求所有标准输入必须在单元测试目标运行之前提供,因此不能交互。
为了提供具体的测试目标示例,请考虑以下猜测游戏代码:
public static void guessingGame() {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int secret = random.nextInt(100) + 1;
System.out.println("I'm thinking of a number from 1 to 100.");
int guess = 0;
while (guess != secret) {
System.out.print("Your guess? ");
guess = scanner.nextInt();
final String[] responses = {"Higher.", "Correct!", "Lower."};
System.out.println(responses[1 + new Integer(guess).compareTo(secret)]);
}
}
现在想象一下一个JUnit测试,它将提供猜测,读取响应以及玩游戏直至完成。在JUnit测试框架中如何做到这一点?
回答:
使用以下安德鲁·查涅斯基(Andrew Charneski)推荐的方法,添加输出刷新(包括System.out.flush();
在上述每个打印语句之后添加),非随机播放以及System.in/out的恢复,此代码似乎执行了我想象中的测试:
@Test
public void guessingGameTest() {
final InputStream consoleInput = System.in;
final PrintStream consoleOutput = System.out;
try {
final PipedOutputStream testInput = new PipedOutputStream();
final PipedOutputStream out = new PipedOutputStream();
final PipedInputStream testOutput = new PipedInputStream(out);
System.setIn(new PipedInputStream(testInput));
System.setOut(new PrintStream(out));
new Thread(new Runnable() {
@Override
public void run() {
try {
PrintStream testPrint = new PrintStream(testInput);
BufferedReader testReader = new BufferedReader(
new InputStreamReader(testOutput));
assertEquals("I'm thinking of a number from 1 to 100.", testReader.readLine());
int low = 1, high = 100;
while (true) {
if (low > high)
fail(String.format("guessingGame: Feedback indicates a secret number > %d and < %d.", low, high));
int mid = (low + high) / 2;
testPrint.println(mid);
testPrint.flush();
System.err.println(mid);
String feedback = testReader.readLine();
if (feedback.equals("Your guess? Higher."))
low = mid + 1;
else if (feedback.equals("Your guess? Lower."))
high = mid - 1;
else if (feedback.equals("Your guess? Correct!"))
break;
else
fail("Unrecognized feedback: " + feedback);
}
} catch (IOException e) {
e.printStackTrace(consoleOutput);
}
}
}).start();
Sample.guessingGame();
}
catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
System.setIn(consoleInput);
System.setOut(consoleOutput);
}
使用PipedInput / OutputStream,例如
final PrintStream consoleOutput = System.out;
final PipedOutputStream testInput = new PipedOutputStream();
final PipedOutputStream out = new PipedOutputStream();
final PipedInputStream testOutput = new PipedInputStream(out);
System.setIn(new PipedInputStream(testInput));
System.setOut(new PrintStream(out));
new Thread(new Runnable() {
@Override
public void run() {
try {
PrintStream testPrint = new PrintStream(testInput);
BufferedReader testReader = new BufferedReader(
new InputStreamReader(testOutput));
while (true) {
testPrint.println((int) (Math.random() * 100));
consoleOutput.println(testReader.readLine());
}
} catch (IOException e) {
e.printStackTrace(consoleOutput);
}
}
}).start();
guessingGame();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句