views:

136

answers:

1

I'm having trouble making assert_raise recognize java exceptions.

I can do

assert_raise(NativeException) { @iter.next }

which works fine, but if I try to get more specific

java_import 'java.util.NoSuchElementException'
#...
assert_raise(NoSuchElementException) { @iter.next }

I get the error

Should expect a class of exception, Java::JavaUtil::NoSuchElementException.
<nil> is not true.

However, I can use begin/rescue/end to catch the exception:

assert(begin
         @iter.next
         false
       rescue NoSuchElementException
         true
       end)

Is there something I'm doing wrong, or is this a failure on Test::Unit's part?

+1  A: 

I would raise it as a bug. It seems it cannot understand the java class when it raised in a block, since it returns nil and therefore, fails the test.

I ran it under jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.5.0_22) [i386-java]

include Java
import java.util.NoSuchElementException
require 'test/unit'

class FooBar < Test::Unit::TestCase
  def test_foo
    exception_caught = false
    begin
      raise NoSuchElementException.new("Bad param")
    rescue NoSuchElementException => e
     exception_caught = true
    end
   assert exception_caught
 end

  def test_bar
    assert_raise NoSuchElementException do
      raise NoSuchElementException.new("Bad param")
    end
  end
end
Pran