我有一个应用程序,当执行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/