Question

I would love to be able to test java code with rspec under jruby, but can't see how to set expectations on internal java method calls. Given the following java:

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

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

I would love to be able to do:

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

Is it possible to integrate a java mocking tool behind the scenes to do this? Has someone already done so? I don't care if I have to use different syntax to set the expectation (instead of rspec's 'should_receive'), but it should at least be concise.

Was it helpful?

Solution

JtestR does exactly what you want. It is a collection of Ruby libraries bundled together with JRuby integration so that running tests is totally painless to set up.

It bundles Mocha and RSpec http://jtestr.codehaus.org/Mocks. For example Rspec for Map, you can write mocks like that:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top