RSpec Stubbing Java Static Method ArgumentError

282 Views Asked by At

I am attempting to stub a Java static method in my specs for my JRuby class which imports some Java libraries. I get the following error for the method call:

Failure/Error: JCoDestinationManager.getDestination('properties')          
ArgumentError:                                               
  Wrong number of arguments. Expected 0, got 1.

That's the JCo Java static method. I created a small spec file to try and isolate the issue:

require 'rails_helper'                                                                                                      

describe SapClient do                                                                                                              
  let(:destination) { double(Java::ComSapConnJcoRt::RfcDestination) }                                                              

  before do                                                                                                                        
    allow(JCoDestinationManager).to receive(:getDestination).and_return destination                                                
  end                                                                                                                              

  it 'can be created' do                                                                                                           
    c = SapClient.new                                                                                                              
    expect(c).to_not be_nil                                                                                                        
  end                                                                                                                              
end

If I add the with clause like so, I still get the same result.

allow(JCoDestinationManager).to receive(:getDestination).with('properties').and_return destination

The strange part to me is that I only encounter this project in rails. If I copy the code over to a Sinatra project and run these specs there, all is well. I created a new Rails project and a new Sinatra project and installed the same versions of RSpec, verified RSpec mocks were the same version, etc. and still saw the discrepancy in behavior.

When I throw in a pry and observe what happens when I just call

JCoDestinationManager.getDestination

With no arguments, I get the mock destination that I define. If I comment out the allow statement, I see a real RfcDestination get created.

While searching, only thing I saw that seemed close to what I'm observing here is from https://github.com/rspec/rspec-mocks/issues/919 but apparently the solution to this problem was a jruby issue that has since been fixed.

Rails Gemfile

Rails Gemfile.lock

Sinatra Gemfile

Sinatra Gemfile.lock

0

There are 0 best solutions below