如何使用 Cucumber 和 Aruba 测试交互式 Java 应用程序

发布于 2024-10-27 06:15:03 字数 884 浏览 1 评论 0原文

如何使用 Cucumber 和 Aruba 测试交互式 Java 应用程序

我有以下 Cucumber/Aruba 场景:

Scenario: Simple echo
  Given I run `java -cp /bin Echo` interactively
  When I type "something\n"
  Then the output should contain "something"

要测试这个简单的 Java 程序:

import java.util.*;
class Echo {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.println(stdin.next());
    }
}

当我运行测试时,它失败并收到以下错误:

Then the output should contain "something"
    expected "" to include "something" (RSpec::Expectations::ExpectationNotMetError)
    features/cli.feature:15:in `Then the output should contain "something"'

当我使用此 ruby​​ 程序尝试相同的测试时:

puts gets

都是绿色的...

我在这里做错了什么吗?

是否必须使用另一种方法从标准输入读取? System.in 仅用于键盘吗?

How to test an interactive java application using cucumber and aruba

I have this Cucumber/Aruba scenario:

Scenario: Simple echo
  Given I run `java -cp /bin Echo` interactively
  When I type "something\n"
  Then the output should contain "something"

To test this simple java program:

import java.util.*;
class Echo {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.println(stdin.next());
    }
}

When I run the test it fails and I get the following error:

Then the output should contain "something"
    expected "" to include "something" (RSpec::Expectations::ExpectationNotMetError)
    features/cli.feature:15:in `Then the output should contain "something"'

When I try the same test with this ruby program:

puts gets

is all green...

Am I doing something wrong here?

Do have to use another method to read from standard input? is System.in used for the keyboard only?

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

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

发布评论

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

评论(1

无声静候 2024-11-03 06:15:03

您可以尝试以下几件事:

首先测试 Cucumber/Aruba 是否正在通过强制输出达到您在测试中的预期来接收您的 System.out.println 调用:即

System.out.println("something");

如果有效,那么您预感读取标准输入是正确(我从未见过有人按照您的方式从标准输入中读取内容)。下面的答案是我通常看到人们如何从 Java 中的标准输入读取内容:

如何在 Java 中读取多行输入

A couple of things you could try:

First test that Cucumber/Aruba is picking up your System.out.println call by forcing the output to be what you expect in your test: i.e.

System.out.println("something");

If that works, then you hunch on reading standard input is correct (i have never seen someone read from standard input the way you are doing it). The answer below is how I typically see people read from standard input in Java:

How to read input with multiple lines in Java

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