4

我希望能够在 jruby 下使用 rspec 测试 java 代码,但看不到如何设置对内部 java 方法调用的期望。给定以下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”),但它至少应该简洁。

4

1 回答 1

2

JtestR 完全符合您的要求。它是与 JRuby 集成捆绑在一起的 Ruby 库的集合,因此运行测试完全可以轻松设置。

它捆绑了 Mocha 和 RSpec http://jtestr.codehaus.org/Mocks。例如 Rspec for Map,你可以像这样编写模拟:

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

更多信息在这里

于 2011-02-15T00:55:57.373 回答