【发布时间】:2017-11-30 12:02:42
【问题描述】:
我在弹簧控制器方法中加载了DatabaseDrivenSpec.groovy。但我不知道如何在 groovy 脚本中调用方法。有人可以给我建议吗?
@Controller
@RequestMapping("/spock")
public class PmsTreeConfluentService {
private final Log logger = LogFactory.getLog(PmsTreeConfluentService.class);
@RequestMapping(value = "/test/spock", method = RequestMethod.GET)
public @ColumnResponseBody
List runTestMock() throws InstantiationException, IllegalAccessException, CompilationFailedException, IOException {
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
File sourceFile = new File("test/groovy/DatabaseDrivenSpec.groovy");
Class testGroovyClass = classLoader.parseClass(new GroovyCodeSource(sourceFile));
GroovyObject instance = (GroovyObject)testGroovyClass.newInstance();//proxy
// instance.invokeMethod(arg0, arg1)
instance = null;
testGroovyClass = null;
return null;
}
}
与导游http://docs.groovy-lang.org/latest/html/documentation/guide-integrating.html
def binding = new Binding()
def engine = new GroovyScriptEngine([tmpDir.toURI().toURL()] as URL[])
while (true) {
def greeter = engine.run('ReloadingTest.groovy', binding)
println greeter.sayHello()
Thread.sleep(1000)
}
我只想访问 http://127.0.0.1:8080/spock/test/spock 然后运行 DatabaseDrivenSpec.groovy 测试用例。
【问题讨论】:
-
您的 PmsTreeConfluentService 看起来像生产代码,通常不依赖(也称为导入)测试代码。你想完成什么?
-
对不起,这是一个java方法。我只是以这种格式粘贴它,它不能。当我定义上面的代码时。我只想访问127.0.0.1:8080/spock/test/spock 然后运行 DatabaseDrivenSpec.groovy 测试用例
-
请问为什么?您还需要从文件系统而不是 jar 加载测试吗?
-
是的,我们确实需要从文件或数据库中加载测试脚本。不是罐子。 groovy 脚本可以热部署。我们想要一个在线测试,而不仅仅是自动开发测试和在 Web 控制台上测试。