2

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?

4

1 回答 1

1

我会把它作为一个错误提出来。当它在一个块中引发时,它似乎无法理解 java 类,因为它返回 nil,因此测试失败。

我在 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
于 2010-02-12T05:30:02.667 回答