我如何在java方法

时间:2017-06-27 12:21:29

标签: groovy spock

我在spring控制器方法中加载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测试用例。

1 个答案:

答案 0 :(得分:5)

如果您想以编程方式运行spock规范,可以尝试这样的方法:

import spock.util.EmbeddedSpecRunner

EmbeddedSpecRunner runner = new EmbeddedSpecRunner()
// There is a lot of runXXX methods, use the apropriate one
runner.runXXXX(<Class of test: testGroovyClass> or <String of test code>) 

Spock基于Junit Runners,请参阅here和一些示例代码here

并且,我不知道您尝试解决的问题,但我强烈建议您使用已经可用于此目的的工具来运行测试。比如Jenkins