Setting upp integration tests that depends on background process can be a challenge in Gradle. Here is one solution that I have used:
gradle.build
apply plugin: 'java'
/**
* Handle of the background process (script scope)
*/
Process backendProcess
/**
* Task to start the Spring server
*/
task beforeE2eTests {
ProcessBuilder builder
builder = new ProcessBuilder('./gradlew bootRun'.split(' '))
builder.redirectErrorStream(true)
doLast {
println "Starting backend"
backendProcess = builder.start()
InputStream sto = backendProcess.getInputStream()
BufferedReader redr = new BufferedReader(new InputStreamReader(sto))
/**
* To prevent Gradle to go to next task before the server has started
* we add a loop that finds a specific log line. When that
* line appears we are good to go to next task
*/
def line
while ((line = redr.readLine()) != null) {
println line
if (line.contains("Started WebApplication")) {
println "Backend is ready"
break;
}
}
}
finalizedBy 'afterE2eTests'
}
/**
* Task to stop the Spring server
*/
task afterE2eTests {
doLast {
println "Stopping backend"
backendProcess.destroy()
}
mustRunAfter 'testAngularE2e'
}
/**
* Task to start E2E tests
*/
task testAngularE2e(type: Exec) {
mustRunAfter 'beforeE2eTests'
/**
* Run the Protractor tests
*/
commandLine 'node_modules/.bin/protractor', 'e2e/protractor.conf.js'
}
/**
* Main testing task
*/
testAll {
dependsOn beforeE2eTests, testAngularE2e
}
Tested on OSX 10.15.0 and Gradle 4.10.2
Comments are closed.