在Gradle中运行测试之前,在单独的线程中执行java程序 [英] Execute java program in separate thread before running tests in Gradle

查看:182
本文介绍了在Gradle中运行测试之前,在单独的线程中执行java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an application that when the main method is executed, it starts a web server to host some RESTful services (using Dropwizard). I'm trying to write tests that access the HTTP methods (rather than the Java methods), so the tests have a prerequisite that the server is running.

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

Here is my task that executes the application and starts the web server:

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

服务器也需要几秒时间启动。粗略地说,我想要做的是这样的:

The server takes a few seconds to start up, too. Roughly, what I want to do is something like this:

test.doFirst {
    println "Starting application..."
    Thread.startDaemon {
        // What goes here???
    }
    sleep 20000
    println "Application should be started."
}

换句话说,在运行测试之前,在一个单独的线程中启动应用程序,等待一段时间再运行测试,给它时间来完成启动。

In other words, before running tests, start the application in a separate thread and wait some time before running tests, giving it time to finish starting up.

这就是说,我无法弄清楚Thread.startDaemon( tasks.run.execute()不起作用),如果这甚至是最好的方法。

That said, I can't figure out what goes in Thread.startDaemon (tasks.run.execute() doesn't work), nor if this is even the best approach. What would be the best way of going about this?

谢谢!

Thanks!

推荐答案

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。这是基本的想法。

Then, when calling Gradle targets, call "gradle.bat startServer testIt" . That is the basic idea.

这篇关于在Gradle中运行测试之前,在单独的线程中执行java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆