zoukankan      html  css  js  c++  java
  • 输入输出流的重定向

    前不久做项目的时候碰到了需要测试用户输入的问题,就是向别人证明用户在控制台输入什么,你就能准确地输出什么。怎么通过测试来模拟这种情况,着实折腾了很久,最后参考别人的做法----输入输出流重定向。

        private PipedOutputStream redirectedInput;

        private OutputStreamWriter redirectedInputWriter;

        private ByteArrayOutputStream redirectedOutput;

        private ByteArrayOutputStream redirectedError;

        private InputStream standardInputStream;

        private PrintStream standardOutputStream;

        private PrintStream standardErrorStream;

     

        /**

         * <p>

         * Sets up the test environment.

         * </p>

         *

         * @throws IOException

         *             to JUnit

         */

        @Override

        protected void setUp() throws IOException {

            // redirects the current standard input / output / error stream

            standardInputStream = System.in;//本来这个输入流是连接键盘

            standardOutputStream = System.out;//本来这个输出流是连接屏幕的

            standardErrorStream = System.err;//这个输出流也是连接屏幕的

     

            // creates the input / output / error stream for testing

            redirectedInput = new PipedOutputStream();

            redirectedInputWriter = new OutputStreamWriter(redirectedInput);

            System.setIn(new PipedInputStream(redirectedInput));

            //现在输入流连接redirectedInputWriter

            //一有东西写入到redirectedInputWriter,就会跑向他的目的地redirectedInput

            //然后再通过管道流pipeInputStream跑到system.in里去,于是模拟了

            //用户输入----程序通过system.in获得用户输入

     

            redirectedOutput = new ByteArrayOutputStream();

            System.setOut(new PrintStream(redirectedOutput));

            //现在输出流连接redirectedOutput

            //一有东西写入到system.out就会跑向他得目的地redirectedOutput

            //以前我们从屏幕读输出信息,现在我们可以从redirectedOutput读信息来验证程序的输出

           

            redirectedError = new ByteArrayOutputStream();

            System.setErr(new PrintStream(redirectedError));

        }

     

        /**

         * <p>

         * Tears down the test environment.

         * </p>

         *

         * @throws IOException

         *             to JUnit

         */

        @Override

        protected void tearDown() throws IOException {

            // closes the input / output / error stream for testing

            redirectedInputWriter.close();

            redirectedInput.close();

            System.in.close();

     

            redirectedOutput.close();

            System.out.close();

     

            redirectedError.close();

            System.err.close();

     

            // redirects the previous standard input / output / error stream

            System.setIn(standardInputStream);

            System.setOut(standardOutputStream);

            System.setErr(standardErrorStream);

     

        }

  • 相关阅读:
    测试amqplib实例,报错 Error: connect ECONNREFUSED 127.0.0.1:5672
    启动vue项目,npm run dev服务起不来报错Error: listen EACCES 0.0.0.0:8080
    win10上安装Docker
    mongodb存储过程
    Versions 出现 SVN Working Copy xxx locked
    Mac OSX Versions输入username按1下都会出现2个字符,并且不能create,解决方法
    Mac OSX 安装nvm(node.js版本管理器)
    jade模板引擎学习笔记(WebsStorm9.0.3+ nodejs+express+jade)
    asp.net首页设置
    两个大数组foreach,找出相同的key数量,所用的时间对比
  • 原文地址:https://www.cnblogs.com/ITEagle/p/1774879.html
Copyright © 2011-2022 走看看