简体   繁体   中英

How to run integration task after run task in gradle?

My Application is Spring boot application exposing some rest api

To run my integration tests , , first the application need to be up and running , as my application gradle based , how to make sure when i execute gradle command from command prompt , first application run and the integration tests will run .

task integration(type: Test, description: 'Runs the integration tests.', group: 'Verification') {
    testClassesDir = sourceSets.integration.output.classesDir
    classpath = sourceSets.integration.runtimeClasspath
    outputs.upToDateWhen { false }
}

task appRunAndIntegrtationTest {
    dependsOn 'run'
    dependsOn 'integration'
    tasks.findByName('integration').mustRunAfter 'run'
}

i added above code to build.gradle , but application up and running , thats it , it stayed their only , integration tests are not running , can anyone has idea on this please .

update : @Strelok , as mentioned , the application started up and integration task is not running .

update 1 : i found one gradle plugin

https://github.com/marc0der/gradle-spawn-plugin

i am trying to use like below

task startServer(type: SpawnProcessTask, dependsOn: 'assemble') {
    command "java -jar ${projectDir}/build/libs/example.jar"
    ready 'Started Application'
}

task stopServer(type: KillProcessTask)

but am getting below exception

*> Could not get unknown property 'SpawnProcessTask' for root project 'example-api' of type org.gradle.api.Project. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.*

please someone please suggest on this

You should declare that the integration test task needs to depend on your app run task.

appRun is just an example please use the name of the task that is the precursor for the integration test.

integration.dependsOn appRun
integration.mustRunAfter appRun

Also, it might be like your app is running and blocking the progress of your Gradle build, does the build actually finish or it just hangs until the app stops running?

I suggest a different approach: start and stop your application from your test framework. Test frameworks support setup and cleanup steps for your test suite (eg BeforeClass and AfterClass in JUnit), start the application in the setup step and stop it in the cleanup step. This approach will make your tests more self-contained, their success/failure will not depend on factors outside the test code.

Even if you prefer to run the application outside the test framework, I suggest wrapping this logic (ie starting the app, running the tests, stopping the app) in a Java class, and executing this class from Gradle via a task of type JavaExec . It will be much more clear than handling all this via Gradle tasks.

Finally, if you still insist on Gradle tasks, it's like the commenters said: the "run" task probably blocks execution while the app is running. Tha only sane way to handle this is to have a task that starts the app in the background, and another that stops it after the tests finished (use finalizedBy ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM