2010-07-04 47 views
4

我希望能夠在jruby下使用rspec測試java代碼,但無法看到如何設置對內部java方法調用的期望。鑑於以下Java:在jruby上設置rspec對內部java方法調用的期望

public class A { 
    public String hi() { 
    return hello(); 
    } 

    public String hello() { 
    return "yo"; 
    } 
} 

我很想能夠做到:

describe 'A' do 
    it 'should call hello' do 
    a = some.java.package.A.new 
    a.should_receive(:hello).and_return('yello') 
    a.hi 
    end 
end 

是否可以集成在幕後一個java嘲諷工具來做到這一點?有人已經這樣做了嗎?我不在乎是否必須使用不同的語法來設置期望值(而不是rspec的'should_receive'),但它至少應該是簡潔的。

回答

2

JtestR正是你想要的。它是與JRuby集成捆綁在一起的Ruby庫的集合,因此運行測試完全無需設置。它包含摩卡和RSpec http://jtestr.codehaus.org/Mocks。 例如Rspec的用於地圖,你可以寫嘲笑這樣的:

it "should be able to add an entry to it" do 
    @hash_map.put "foo", "bar" 
    @hash_map.get("foo").should == "bar" 
    end 

More info here

相關問題