我是 Groovy 的新手,正在尝试在我的应用程序中实现 Spock 框架。 这是我的测试代码:
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
}
这里 TestClass
抛出一些异常,我必须在测试方法中捕获或再次抛出它。我试过了
try {
push.exec(sp)
} catch (Exception e) {
}
但还是得到了
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)
最佳答案
而不是 test = TestClass()
,它应该是 test = new TestClass()
。要测试预期的异常,请使用 Specification.thrown
而不是 try-catch。参见 Spock 的 Javadoc举个例子。
关于groovy - 在 Groovy 中抛出/捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15909716/