在Groovy中抛出/捕捉异常 [英] Throw/Catch Exception in Groovy
本文介绍了在Groovy中抛出/捕捉异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Groovy很陌生,试图在我的应用程序中实现Spock框架。
这里是我的测试代码:
defTest class with mock object(){
setup:
SomeObject sp = Mock()
test = TestClass()
当:
System.out.println('come here');
push.exec(sp)
then:
sp.length == 1
}
这里 TestClass
引发了一些我必须在测试方法中捕获的异常或者再次抛出异常。我试过
try {
push.exec(sp)
} catch(Exception e){
}
但仍然可以获得
groovy.lang.MissingMethodException:方法没有签名:test.spock.TestClassTest.TestClass()适用于参数类型:()values:[]
可能的解决方案:使用([Ljava.lang.Object;),使用(java.util.List,groovy.lang.Closure),使用(java.lang.Class,groovy.lang.Closure),转储),(groovy.lang.Closure),each(groovy.lang.Closure)
解决应该是 test < >。要测试预期的异常,请使用
test = new TestClass()
Specification.thrown
代替try-catch。查看Spock的 Javadoc 的示例。 I am new to Groovy and trying to implement Spock framework in my application. Here is my test code:
def "Test class with mock object"() {
setup:
SomeObject sp = Mock()
test= TestClass()
when:
System.out.println('comes here');
push.exec(sp)
then:
sp.length == 1
}
Here TestClass
is throwing some exception which I have to catch in test method or throw it again. I tried
try {
push.exec(sp)
} catch (Exception e) {
}
But still getting
groovy.lang.MissingMethodException: No signature of method: test.spock.TestClassTest.TestClass() is applicable for argument types: () values: []
Possible solutions: use([Ljava.lang.Object;), use(java.util.List, groovy.lang.Closure), use(java.lang.Class, groovy.lang.Closure), dump(), with(groovy.lang.Closure), each(groovy.lang.Closure)
解决方案
Instead of test = TestClass()
, it should be test = new TestClass()
. To test for an expected exception, use Specification.thrown
instead of try-catch. See Spock's Javadoc for an example.
这篇关于在Groovy中抛出/捕捉异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文