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

Pro*_*lum 6 java junit unit-testing

理想情况下,我想编写JUnit测试代码,以交互方式测试学生基于文本的I/O应用程序.使用System.setIn()/ .setOut()导致问题,因为底层流阻塞.Birkner的系统规则(http://www.stefan-birkner.de/system-rules/index.html)在之前的文章(基于测试控制台的应用程序/程序 - 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)]);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在想象一下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);
}
Run Code Online (Sandbox Code Playgroud)

And*_*ski 3

使用 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();
Run Code Online (Sandbox Code Playgroud)