名前空間の理由でFoo
クラス内にBarException
を定義したいとします。コンストラクタが見つからないため、ネストされた例外クラスをインスタンス化できません。
package org.company.utils
class Foo {
class BarException extends RuntimeException { }
def raise() {
def r = new RuntimeException("BOOM")
throw new BarException(r)
}
}
これは私が仕事にそれを好きだろうかです::
void testFoo() {
shouldFail(Foo.BarException) {
def foo = new Foo()
foo.raise()
}
}
しかし、テストが失敗しました:
1) testFoo(test.FooTestCase)java.lang.AssertionError: Closure [email protected] should have failed with an exception of type org.company.utils.Foo$BarException, instead got Exception groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.company.utils.Foo$BarException(org.company.utils.Foo, java.lang.RuntimeException)
私が試してみましたこれは私のFoo
クラスは次のようになりますnew BarException(this, r)
を使用してBarExceptionをインスタンス化し、を約BarException
に置き換えますが、コンパイラでは何も機能しません。
問題が何ですか?
感謝。あなたの答えは私の問題に対処しましたが、可能であれば、なぜ私の考えが機能しないのかを「静的ではない」理由について説明したいと思います。 –
使用しているGroovyのバージョンは? –
'Groovyバージョン:2.4.11 JVM:1.8.0_144ベンダー:Oracle Corporation OS:Mac OS X' –