2011-03-27 101 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