Question: I have seen several post regarding making a build in Jenkins fail if the unit test execution fail (e.g. this [one](https://stackoverflow.com/questions/6681880/how-to-make-jenkins-display-fail-if-one-of-the-ant-tests-fail)). It turns out that by default Jenkins reports builds with failing tests as `unstable` and some people do not like that. This, however, will be perfectly fine for me. I just want to be able to easily differentiate builds with passing tests from such with failing tests. 

And here is the catch: **I am developing for Android** so my build is configured following [this page](https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project). Basically the tests are run with the following command:

```
ant all clean emma debug install test

```

As result coverage report is generated and published in Jenkins.

All posts I have read about configuring the Jenkins result according to tests results
were dealing with ant task manipulation. However, if we look at the android `build.xml` the Android tests are run with adb command: `adb shell am instrument ...`. I don't know how to configure this command to print the tests results. It can be configured to print the coverage report. I have already done that, but was never able to make the build fail according to the coverage report.

I hope somebody else also faced the same problem and managed to solve it. Any guidance will be most appreciated.

Answer: 
Rafiq's response did not work for me - the app hung. I think the reason has to do with the nature of isCancelled(): "Returns true if this task was cancelled before it completed normally." If the task completes normally (i.e. is not cancelled) then `while(!task.isCancelled()) { }` will loop forever.

To solve this create a Boolean flag that you instatiate to `false` and then flip to `true` in `task.onPostExecute()`. Then do `while(!flag) { }` before switching Activities. Additionally, if you'd like to give the main thread a 'break' to let the AsyncTask process a little faster, you can do try this:

```
while (!flag) {
    try { Thread.sleep(100); }
    catch (InterruptedException e) { e.printStackTrace(); }
}

```

It seems to be working well for me.
```
intent = new Intent(this, OrdineCreaActivity.class);
context.startActivityForResult(intent, R.id.buttonPagamenti);

```

Write the above lines in onPostExecute() of you AysncTask. Because if we are using AsyncTask it wont wait there until the task complete.