views:

27

answers:

1

I am testing Java classes with RSpec and JRuby.

How can I stub out a call to super in an imported Java class in my RSpec test?

For example:

I have 2 Java classes:

public class A{
  public String foo() {
    return "bar";
  }
}

public class B extends A
  public String foo() {
    // B code
    return super.foo();
  } 
}

I am just trying to test the code in B.foo and not the code in A.foo with JRuby. How can I stub out the call to the super class method in my RSpec test?

rspec test:

java_import Java::B

describe B do
  it "should not call A.foo" do
    # some code to stub out A.foo
    b = B.new
    b.foo.should_not == "bar"
  end
end

I have tried including a module with a new foo method in B's class hoping that it would hit the module method first but B still makes a call to A. The inserting module technique works in Ruby but not with JRuby and imported Java classes.

Any other ideas to stub out the superclass method to get my RSpec test to pass?

A: 

I don't think you can, and it seems illogical. The foo() method invocation of super.foo() is part of it's implementation - in principle, it might be necessary for it's correct behavior.

Perhaps the functionality you want to test separately needs to be separated into a separate method and called from foo() and by your testing?

Software Monkey
I agree that refactoring for testing might be the easiest solution. Sometimes it's harder to refactor especially if I'm using a vendor library. I am hoping that I can still do this in JRuby as this is an option in Ruby. It be nice to have another tool for testing.
Doug Chew