Writing data to System.in

前端 未结 4 1111
感情败类
感情败类 2020-12-14 21:42

In our application, we expect user input within a Thread as follows :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         


        
4条回答
  •  被撕碎了的回忆
    2020-12-14 22:29

    My solution currently (in 2018) is:

     final byte[] passCode = "12343434".getBytes();
     final ByteArrayInputStream inStream = new ByteArrayInputStream(passCode);
            System.setIn(inStream);
    

    [Update in 2019] For JUnit4 Tests there is a framework for these tasks: https://stefanbirkner.github.io/system-rules/ (the upgrade to JUnit5 is on going: https://github.com/stefanbirkner/system-rules/issues/55)

提交回复
热议问题