【问题标题】:how can i run a spock groovy test in java method我如何在 java 方法中运行 spock groovy 测试
【发布时间】: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 控制台上测试。

标签: groovy spock


【解决方案1】:

如果您想以编程方式运行 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

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多