Problem with Java Exceptions in RSpec
-------------------------------------

                 Key: JRUBY-1127
                 URL: http://jira.codehaus.org/browse/JRUBY-1127
             Project: JRuby
          Issue Type: Bug
          Components: Java Integration
    Affects Versions: JRuby 1.0.0RC3
         Environment: Sun JDK 1.5, Ubuntu Linux
            Reporter: Per Jacobsson


I've been playing around with Rspec and JRuby to try to unit test my Java code. 
It's working well so far, except for when I want the mock objects to throw 
Exceptions.

I have these three simple classes. Client is the one being tested:

public class Client {

   private final Service service;

   public Client(Service service) {
      this.service = service;
   }

   public void execute() {
      try {
         service.doSomething();
      } catch (ServiceException e) {
         e.printStackTrace();
      }
   }
}

public interface Service {
   public void doSomething() throws ServiceException;
}

public class ServiceException extends Exception {
}


And the Rspec definition looks like this:

require 'java'

describe Java::Client do

  before(:each) do
    @service = Java::Service.new
    @client = Java::Client.new(@service)
  end

  it "shouldn't blow up if there's a ServiceException" do
    ex = Java::ServiceException.new
    @service.should_receive(:doSomething).once.and_raise(ex)

    lambda { @client.execute }.should_not raise_error
  end

end

So in this case I want the mock object to throw a ServiceException, the Java 
code to catch it, and the test to verify that nothing escapes. I end
up with what looks like a type mismatch between Ruby and Java exceptions:

expected no Exception, got #<TypeError: exception class/object expected>

If I change the thrown Exception to be a pure Ruby Exception it does not get 
caught by the catch block in the Client class. It gets a
org.jruby.exceptions.RaiseException.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to