Kulkarni Sachin
Kulkarni Sachin

Reputation: 313

How to terminate tests in TestNG ANT task gracefully?

I have built test suite where they invoke builds in Jenkins through HTTP call and monitor the execution status. However whenever I send Ctrl+C, I notice that test execution halts immediately. I want to invoke some clean up code to stop Jenkins build execution before exiting.

I tried using Runtime.getRuntime().addShutdownHook but couldn't get it to working.

Sample Code :

public class BaseAntTask extends TestNGAntTask {

    @Override
    public void execute() throws BuildException {
        // set properties.
        super.execute();
    }

    @Override
    protected int executeAsForked(final CommandlineJava cmd, final ExecuteWatchdog watchdog) {
        // Invoke Jenkins build and other operations. 
        Runtime.getRuntime().addShutdownHook(new Thread(() -> JobApi.stopBuild());
        
        return 1;
    }
}

Upvotes: 0

Views: 26

Answers (0)

Related Questions