OnPostExecute () AsyncTask is never called in AndroidTestCase

I am writing a very simple unit test case for my Android project, the test script just executes AsyncTask, which performs a network operation in the background. I am extending the class AndroidTestCasefor my test case:

public class MyTest extends AndroidTestCase{
  //use CountDownLatch to perform wait-notify behavior
  private CountDownLatch signal;
  @Override
  public void setUp() throws Exception{
     super.setUp();
     //I use CountDownLatch to perform wait-notify behaviour
     signal = new CountDownLatch(1); 
  }

  @Override
  public void runTest() throws Exception{
        String params = "some params";
        //MyAsyncTask does the networking tasks
        new MyAsyncTask().execute(params);

       //wait until MyAsyncTask is done
       try {
        signal.await();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

  }

  private class MyAsyncTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
          //Do networking task e.g. access a remote database
     }

     @Override
    protected void onPostExecute(String result){
        super.onPostExecute(result);
            //this never get called when run this test case, why?
           Log.i("Debug","post execute");       
           signal.countDown();
    }
  }
}

As you see above, I use CountDownLatch to execute the wait-notify behavior .

After starting MyAsicTaskI call signal.await()to wait for MyAsyncTaskto the completion.

In onPostExecute()the callback, MyAsyncTaskI call signal.countDown()in to notify that the task is completed.

, onPostExecute , ?

======== ==========

Thread.sleep(30*1000) runTest(), . , teardown() . tearDown(), Android. .

, wait-notify CountDownLatch ... ?

+4
4

... . , AndroidTestCase, InstrumentationTestCase.

asyncTask :

runTestOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            startAsyncTask();
        }
    });
+1

, , , onPostExecute().

0

, doInBackground . . , , .

private class MyAsyncTask extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.i("Debug", "onPreExecute");
    }

    @Override
    protected String doInBackground(String... params) {
        Log.i("Debug", "doInBackground");
        return "operation finished";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.i("Debug", "onPostExecute");
        signal.countDown();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        Log.i("Debug", "onCancelled");
    }
}

: , AndroidTestCase AsyncTask s. AsyncTask (. ). doInBackground onPostExecute . 2 , onPostExecute. tearDown , onPostExecute.

So, your problem here is probably in a loop inside your method doInBackground. Or it is somehow canceled and the method called onCancelled.

0
source

It is assumed that it onPostExecute()starts in the caller's thread execute(). If this thread pauses, causing something like signal.await(), onPostExecute()it will not be able to work. So you cannot signal.await()after the call execute().

0
source

All Articles