如何使用 Cucumber 和 Aruba 测试交互式 Java 应用程序
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下几件事:
首先测试 Cucumber/Aruba 是否正在通过强制输出达到您在测试中的预期来接收您的 System.out.println 调用:即
如果有效,那么您预感读取标准输入是正确(我从未见过有人按照您的方式从标准输入中读取内容)。下面的答案是我通常看到人们如何从 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.
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