gpt4 book ai didi

java - 一个 JUnit 如何测试交互式的、基于文本的 Java 应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:09:48 25 4
gpt4 key购买 nike

理想情况下,我想编写 JUnit 测试代码,以交互方式测试学生基于文本的 I/O 应用程序。使用 System.setIn()/.setOut() 会导致问题,因为底层流正在阻塞。 Birkner 的系统规则 (http://www.stefan-birkner.de/system-rules/index.html) 在较早的帖子 (Testing console based applications/programs - Java) 中被推荐,但它似乎要求在运行单元测试目标之前提供所有标准输入,因此不是交互式的。

要提供具体的测试目标示例,请考虑这个猜谜游戏代码:

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();

关于java - 一个 JUnit 如何测试交互式的、基于文本的 Java 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com