I am currently working on a PR that solves a small bug in the vertx-gradle-plugin. In order to test my PR, I’d like to test two of the gradle tasks (vertxRun and vertxDebug) using the TestKit. My first clunky draft looks like the following:
@Test(timeout = 20_000)
fun `check that the application does run with custom launcher`() {
val `in` = PipedInputStream()
val reader = BufferedReader(InputStreamReader(`in`))
val out = PipedOutputStream(`in`)
val runnerTask = Thread {
GradleRunner.create()
.withProjectDir(File("src/test/gradle/simple-project"))
.withPluginClasspath()
.withArguments("clean", "vertxRun")
.forwardStdOutput(out.writer())
.build()
}
runnerTask.start()
reader.lineSequence().forEach { line ->
if (line.startsWith("Starting vert.x application...")) {
val response = Unirest.get("http://localhost:18080/").asString()
Unirest.shutdown()
assertThat(response.status).isEqualTo(200)
assertThat(response.body).isEqualTo("Yo!")
return
}
}
fail<String>("Nope!")
}
While this approach seems to work, I wonder if there is a cleaner solution to test a gradle task that is not terminating. I had a look at Spring’s BootRunIntegrationTest and their solution works without using a separate thread and the like, yet I did not figure out what they are doing differently.
I wonder if there is recommended approach to test these tasks?