2011-03-27 96 views
0

如何测试用黄瓜和阿鲁巴如何用黄瓜和阿鲁巴

一个交互式的Java应用程序来测试一个交互的Java应用程序,我有这样的黄瓜/阿鲁巴场景:

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仅用于键盘吗?

回答

0

一对夫妇的事情,你可以尝试:

首先测试的是黄瓜/阿鲁巴拾取通过强制输出你的System.out.println通话是在你测试的预期:即

System.out.println("something"); 

如果这有效,那么你在阅读标准输入是正确的(我从来没有见过有人从标准输入读取你的方式)预感。下面的答案就是我通常看到人们从标准输入Java中阅读:

How to read input with multiple lines in Java

+0

都尝试的事情,仍然没有工作:( – victrnava 2011-03-29 12:21:27

+0

所以的System.out.println(“东西”)仍然没有得到回升并让你的测试通过?听起来像aruba没有拿起Java输出,它是否从System.err.println(“Something”)中获取内容? – Clinton 2011-03-30 00:27:53