groovy - 在Gradle中运行测试之前,请在单独的线程中执行Java程序

标签 groovy gradle

我有一个应用程序,当执行main方法时,它将启动Web服务器来托管一些RESTful服务(使用Dropwizard)。我正在尝试编写访问HTTP方法(而不是Java方法)的测试,因此这些测试具有服务器正在运行的先决条件。

这是我执行应用程序并启动Web服务器的任务:

task run (dependsOn: 'classes', type: JavaExec) {
    main = 'com.some.package.to.SomeService'
    classpath = sourceSets.main.runtimeClasspath
    args 'server', 'some.yml'
}

服务器也需要几秒钟来启动。大致来说,我想做的是这样的:
test.doFirst {
    println "Starting application..."
    Thread.startDaemon {
        // What goes here???
    }
    sleep 20000
    println "Application should be started."
}

换句话说,在运行测试之前,请在单独的线程中启动应用程序,并在运行测试之前等待一段时间,以便有时间完成启动。

就是说,我无法弄清楚Thread.startDaemon中的内容(tasks.run.execute()不起作用),即使这也不是最好的方法。最好的方法是什么?

谢谢!

最佳答案

我可能会做的是这样的:

task startServer (type: Exec) {
    workingDir 'tomcat/bin'
    // using START hopefully forks the process
    commandLine 'START', 'start.bat'
    standardOutput = new ByteArrayOutputStream()
    ext.output = {
      return standardOutput.toString()
    }
    // loop through output stream for finished flag
    // or just put a timeout here
}

task testIt (type: Test) {
    description "To test it."
    include 'org/foo/Test*.*'
}

然后,在调用Gradle目标时,调用“gradle.bat startServer testIt”。这是基本思想。

关于groovy - 在Gradle中运行测试之前,请在单独的线程中执行Java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16703083/

相关文章:

使用Groovy进行JSON输出

jenkins - Jenkins Groovy 脚本中的预期步骤

java - Android 以编程方式从 jar 中检索许可证信息

java - 如何从 Gradle 中的 JAR 中排除资源并通过 IntelliJ 运行

android - 仅在一次构建中清除特定数据

java - android studio gradle问题:找不到com.android.tools.build:gradle:3.5.0

unit-testing - java.lang.IncompleteClassChangeError org.junit.jupiter.params.provider.Arguments 必须是 InterfaceMethodref 常量

function - 'f(x) { return x }' 与 'f = { x -> x }' 有何不同?

java - 使用 spock 在测试中生成对象列表

function - Gradle-使用Ant中的函数