2010-02-02 4 views
2

Je rencontre des problèmes pour reconnaître assert_raise les exceptions java.JRuby et Test :: Assert_raise de l'unité

je peux faire

assert_raise(NativeException) { @iter.next } 

qui fonctionne très bien, mais si je tente d'être plus précis

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

Je reçois l'erreur

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

Cependant, je peux utiliser begin/rescue/end pour attraper l'exception:

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

Y a-t-il quelque chose que je fais mal, ou est-ce une erreur de la part de Test::Unit?

Répondre

1

Je l'évoquerais comme un bug. Il semble qu'il ne peut pas comprendre la classe java quand il a soulevé dans un bloc, car il renvoie zéro et, par conséquent, échoue le test.

je l'ai couru sous JRuby 1.4.0 (rubis 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 
+1

JRuby 1.7.3 avec openjdk-7: "2 tests, 2 assertions, 0 échecs, 0 erreurs, 0 sauts" –