我如何在Java方法中运行Spock Groovy测试

Der*_*rek 2 groovy spock

我在弹簧控制器method.load中加载了DatabaseDrivenSpec.groovy,但是我不知道如何在Groovy脚本中调用invokeMethod方法。有人可以给我建议吗?

@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;
    }               
}
Run Code Online (Sandbox Code Playgroud)

与指南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)
}
Run Code Online (Sandbox Code Playgroud)

我只想访问http://127.0.0.1:8080/spock/test/spock,然后运行DatabaseDrivenSpec.groovy测试用例。

raf*_*lim 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>) 
Run Code Online (Sandbox Code Playgroud)

斯波克是基于Junit的运动员,看到它在这里和一些示例代码在这里

而且,我不知道您要解决的问题,但我强烈建议您使用为此目的已经可用的工具运行测试。像詹金斯的例子。