将数据写入System.in

发布于 2024-09-24 22:38:26 字数 231 浏览 3 评论 0 原文

在我们的应用程序中,我们期望 Thread 中的用户输入如下:

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

我想在单元测试中传递该部分,以便我可以恢复线程来执行其余代码。如何从 junit 向 System.in 写入内容?

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

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

I want to pass that part in my unit test so that I can resume the thread to execute the rest of the code. How can I write something into System.in from junit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请止步禁区 2024-10-01 22:38:26

您想要做的是使用方法 setIn() 来自 系统。这将允许您将数据从 junit 传递到 System.in 中。

What you want to do is use the method setIn() from System. This will let you pass data into System.in from junit.

未央 2024-10-01 22:38:26

在测试期间更换它:

String data = "the text you want to send";
InputStream testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
InputStream old = System.in;
try {
    System.setIn( testInput );

    ...
} finally {
    System.setIn( old );
}

Replace it for the duration of your test:

String data = "the text you want to send";
InputStream testInput = new ByteArrayInputStream( data.getBytes("UTF-8") );
InputStream old = System.in;
try {
    System.setIn( testInput );

    ...
} finally {
    System.setIn( old );
}
赠意 2024-10-01 22:38:26

除了上面的建议(编辑:我注意到 Bart 也在评论中留下了这个想法),我建议通过让类接受输入源作为构造函数参数来使你的类更具单元测试性或类似的(注入依赖项)。无论如何,类不应该与 System.in 如此耦合。

如果您的类是从 Reader 构造的,您可以这样做:

class SomeUnit {
   private final BufferedReader br;
   public SomeUnit(Reader r) {
       br = new BufferedReader(r);
   }
   //...
}

//in your real code:
SomeUnit unit = new SomeUnit(new InputStreamReader(System.in));

//in your JUnit test (e.g.):
SomeUnit unit = new SomeUnit(new StringReader("here's the input\nline 2"));

Instead of the suggestions above (edit: I noticed that Bart left this idea in a comment as well), I would suggest making your class more unit testable by making the class accept the input source as a constructor parameter or similar (inject the dependency). A class shouldn't be so coupled to System.in anyway.

If your class is constructed from a Reader, you can just do this:

class SomeUnit {
   private final BufferedReader br;
   public SomeUnit(Reader r) {
       br = new BufferedReader(r);
   }
   //...
}

//in your real code:
SomeUnit unit = new SomeUnit(new InputStreamReader(System.in));

//in your JUnit test (e.g.):
SomeUnit unit = new SomeUnit(new StringReader("here's the input\nline 2"));
维持三分热 2024-10-01 22:38:26

我当前(2018 年)的解决方案是:

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

[2019 年更新] 对于 JUnit4 测试,有一个用于这些任务的框架:
https://stefanbirkner.github.io/system-rules/
(JUnit5 的升级正在进行中:https://github.com/stefanbirkner/system -规则/问题/55

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文