Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.5.6
-
Fix Version/s: JRuby 1.7.0.pre1
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
assert_raise in test/unit does not properly handle NativeExceptions. When given one to test against, the code in _check_exception_class checks if the class inherits from the ruby Exception class (with `assert(Exception >= klass,...`). This fails when a NativeException is passed into it. A similar bug was made earlier as JTESTR-45, but the reporter focused on rspec (which was found to be invalid) and did not follow through with test/unit and the bug was not elevated to JRUBY. Also, this seems to have been known on the web at http://stackoverflow.com/questions/2187166/jruby-and-testunits-assert-raise with no resolution.
This code reproduces the problem:
require 'test/unit'
require 'java'
class TestAssertRaiseWithNativeException < Test::Unit::TestCase
java_import('java.util.NoSuchElementException') { 'FooError' }
java_import 'java.util.MissingResourceException'
def test_native_exception_with_renaming
assert_raise(FooError) {
raise FooError
}
end
def test_native_exception
assert_raise(Java::JavaUtil::MissingResourceException) {
raise Java::JavaUtil::MissingResourceException
}
end
end
The above tests will fail with these results:
1) Failure: test_native_exception(TestAssertRaiseAliasedNativeException) [assert_raise_problem.rb:15]: Should expect a class of exception, Java::JavaUtil::MissingResourceException. <nil> is not true. 2) Failure: test_native_exception_with_renaming(TestAssertRaiseAliasedNativeException) [assert_raise_problem.rb:8]: Should expect a class of exception, Java::JavaUtil::NoSuchElementException. <nil> is not true.
Noticed that this test case will not pass properly because the `raise` keyword in jruby does not handle native exception classes.
Changing `raise FooError` to `raise FooError.new` and `raise Java::JavaUtil::MissingResourceException` to `raise Java::JavaUtil::MissingResourceException.new('f','f','f')` will have tests that will correctly pass when _check_exception_class is patched.