当前位置: 首页 > 知识库问答 >
问题:

如何在java方法中运行spock groovy测试

阎知
2023-03-14

我在Spring控制器中加载数据库驱动pec.groovymethod.but我不知道如何调用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然后运行数据库驱动pec.groovy测试用例。

共有1个答案

宰父飞翼
2023-03-14

如果希望以编程方式运行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 Runner的,请看这里和一些示例代码

而且,我不知道您试图解决的问题,但是我强烈建议使用已经可用的工具运行您的测试。比如詹金斯。

 类似资料:
  • 问题内容: 我想执行以特定顺序注释的测试方法。 例如: 我想确保每次运行前都运行,但是找不到类似的注释。 我认为对于JUnit来说这是非常重要的功能,如果JUnit的作者不希望使用订单功能,为什么呢? 问题答案: 我认为这对于JUnit来说是非常重要的功能,如果JUnit的作者不希望使用订购功能,为什么呢? 我不确定用JUnit做到这一点的干净方法,据我所知JUnit假定所有测试都可以按任意顺序执

  • 这是驱动程序类,它将为每个测试方法创建驱动程序实例。

  • 我想运行我的gui,但是我不知道如何初始化这个类。我正在从python过渡到Java,所以我对这个还是相当陌生。代码有效,我只需要知道如何运行它。 如何在静态void main(String[] args)下运行init()方法?

  • 我想知道是否可以在java spring boot中使用调度或spring作业只运行一次方法?我用温度变量作为开关,但我正在寻找更好更干净的方法。

  • 我有很多用Java编写的cucumber .feature文件。 看起来它们是并行运行的。 我如何以单线程的方式一个接一个地运行cucumber测试? 运行测试配置为:

  • 问题内容: 一般问题是: 使用语法时,可以将其转换为功能接口等类型吗? 如果不是,那么如何在Java 8中间接调用方法引用? 举例来说,假设我们想要一个as: 它可以用作: 在以上代码段中,一种选择是但非常严格。正如这个答案所提到的,在b75版本之前,有一个可用的并且可能很方便。 另一方面,任何可能以某种方式转换为的机会? __Java专家 注意事项 :请根据需要改进问题的标题。 问题答案: 我认