我需要从一个String创建一个Script,并在当前测试类的上下文中执行它。
import spock.lang.Specification
class MyTestSpec extends Specification {
Integer getOne() { return 1 }
Integer getTwo() { return 2 }
void 'call script with local methods'() {
given:
GroovyShell shell = new GroovyShell()
Script script = shell.parse("getOne() + getTwo()")
when:
def result = script.run()
then:
result == 3
}
}
这会产生以下错误:
No signature of method: Script1.getOne() is applicable for argument types: () values: []
我看到可以使用shell.setProperty
来设置变量,但是如何将方法的实现传递给脚本呢?
1条答案
按热度按时间kg7wmglp1#
当然,我一贴出这个,就找到了我的答案。