2

assert_raiseJava例外を認識させるのに問題があります。

できます

assert_raise(NativeException) { @iter.next }

これは問題なく動作しますが、より具体的にしようとすると

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

エラーが発生します

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

ただし、begin/rescue/end例外をキャッチするために使用できます。

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

私が間違っていることはありますか、それともこれはの失敗Test::Unitですか?

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 に答える