我想爲類名稱間距原因在Foo
類中定義BarException
。這是我的Foo
類的樣子:由於未找到構造函數而無法實例化嵌套的異常類
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)
我曾嘗試使用new BarException(this, r)
實例化BarException,並將groovy.transform.InheritConstructors
放在BarException
左右,但對編譯器無效。
有什麼問題?
謝謝。你的回答解決了我的問題,但如果可能的話,我還想解釋爲什麼沒有'靜態'我的想法不起作用。 –
您使用的是什麼版本的groovy? –
'Groovy版本:2.4.11 JVM:1.8.0_144供應商:Oracle Corporation操作系統:Mac OS X' –