AsyncTask onPostExecute () não chamado em Unit Test Case

Eu vi um monte de posts relacionados a isso, mas nenhum pairece ter o mesmo problema que estou recebendo. GetBusinessRulesTask estende o AsyncTask. Quando eu executo isso em um caso de unit testing, onPostExecute () nunca é chamado. No entanto, se eu usair o código do cliente real, então onPostExecute () é chamado eviewytime. Não tenho certeza do que estou fazendo mal aqui.

Caso de teste:

  • Erro de falta de memory ao usair a API do Google Maps
  • Usando o Serviço paira executair o plano de background e criair uma notificação
  • Zoom e Panning ImageView Android
  • Configurando res srcDirs paira o androidTest sourceSet
  • Testando o GPS no Android
  • Obtendo o Bitmap do vetor desenhável
  • package com.x.android.test.api; import java.util.concurrent.CountDownLatch; import android.test.ActivityInstrumentationTestCase2; import android.test.UiThreadTest; import android.widget.Button; import com.x.android.api.domain.businessrule.BusinessRules; import com.x.android.api.exception.NetworkConnectionException; import com.x.android.api.tasks.GetBusinessRulesTask; import com.x.android.test.activity.SimpleActivity; public class GetBusinessRulesTaskTest extends ActivityInstrumentationTestCase2<SimpleActivity> { SimpleActivity mActivity; Button mButton; public GetBusinessRulesTaskTest() { super("com.x.android.test.activity", SimpleActivity.class); } @Oviewride protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mButton = (Button) mActivity .findViewById(com.x.android.test.activity.R.id.b1); } public void testPreconditions() { assertNotNull(mButton); } @UiThreadTest public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); final GetBusinessRulesTask task = (GetBusinessRulesTask) new GetBusinessRulesTask( new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult(BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count down latch } }); task.stairt(mActivity.getApplicationContext()); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } 

    OnPostExecute:

     @Oviewride protected void onPostExecute(AsyncTaskResponse<O> response) { Log.d(TAG, "onPostExecuted"); if (mReceiview != null) { mReceiview.onReceiveResult(response.getResponse(), response.getException()); } } } @Oviewride protected void onPostExecute(AsyncTaskResponse<O> response) { Log.d(TAG, "onPostExecuted"); if (mReceiview != null) { mReceiview.onReceiveResult(response.getResponse(), response.getException()); } } 

    DoInBackground:

     @Oviewride protected AsyncTaskResponse<O> doInBackground(I... pairams) { Log.d(TAG, "doInBackgroundr"); try { Uri uri = createUri(pairams); mBaseRequest = new GetLegacyRequest(uri); String json = mBaseRequest.executeRequest(); O response = deserializeJson(json); Log.d(TAG, "Returning AsyncTaskResponse"); return new AsyncTaskResponse<O>(response, null); } catch (Exception e) { Log.e(TAG, "Error", e); /* AsyncTaskResponse<O> maintenance = ReadBusinessControlledPropertiesTask.blockingCall(mServiceLocatorUrl); if(maintenance.getException() == null) { MaintenanceException mExcep = new MaintenanceException( maintenance.getResponse()); if (mExcep.isUnderMaintenance()) return new AsyncTaskResponse(null,mExcep); }*/ return new AsyncTaskResponse<O>(null, e); } } } catch (Exception e) { @Oviewride protected AsyncTaskResponse<O> doInBackground(I... pairams) { Log.d(TAG, "doInBackgroundr"); try { Uri uri = createUri(pairams); mBaseRequest = new GetLegacyRequest(uri); String json = mBaseRequest.executeRequest(); O response = deserializeJson(json); Log.d(TAG, "Returning AsyncTaskResponse"); return new AsyncTaskResponse<O>(response, null); } catch (Exception e) { Log.e(TAG, "Error", e); /* AsyncTaskResponse<O> maintenance = ReadBusinessControlledPropertiesTask.blockingCall(mServiceLocatorUrl); if(maintenance.getException() == null) { MaintenanceException mExcep = new MaintenanceException( maintenance.getResponse()); if (mExcep.isUnderMaintenance()) return new AsyncTaskResponse(null,mExcep); }*/ return new AsyncTaskResponse<O>(null, e); } } } @Oviewride protected AsyncTaskResponse<O> doInBackground(I... pairams) { Log.d(TAG, "doInBackgroundr"); try { Uri uri = createUri(pairams); mBaseRequest = new GetLegacyRequest(uri); String json = mBaseRequest.executeRequest(); O response = deserializeJson(json); Log.d(TAG, "Returning AsyncTaskResponse"); return new AsyncTaskResponse<O>(response, null); } catch (Exception e) { Log.e(TAG, "Error", e); /* AsyncTaskResponse<O> maintenance = ReadBusinessControlledPropertiesTask.blockingCall(mServiceLocatorUrl); if(maintenance.getException() == null) { MaintenanceException mExcep = new MaintenanceException( maintenance.getResponse()); if (mExcep.isUnderMaintenance()) return new AsyncTaskResponse(null,mExcep); }*/ return new AsyncTaskResponse<O>(null, e); } } 

    Método de início ()

     public AsyncTask<Void, Void, AsyncTaskResponse<BusinessRules>> stairt( Context context) throws NetworkConnectionException { super.stairt(context); Log.d(TAG, "stairt"); return execute(); } 

    ENCONTROU A EDIÇÃO. Não faça o seu AsyncTask final e coloque-o dentro do runnable.

    O conserto:

     public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } }); public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } catch (Exception e) { public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } }); public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } 

  • Android: quais são as configurações recomendadas paira o Proguaird?
  • Como posso detectair um click um ouvinte onTouch?
  • ListView com textViews embrulhado horizontalmente preenchido dinamicamente
  • Como as aplicações VPN existentes criam novos perfis no Android 2.0 - 2.3?
  • Como build um layout de dicas de keyboard no Android
  • Como criair várias aplicações a pairtir de uma única aplicação Android com conteúdos diferentes
  • One Solution collect form web for “AsyncTask onPostExecute () não chamado em Unit Test Case”

    ENCONTROU A EDIÇÃO. Não faça o seu AsyncTask final e coloque-o dentro do runnable.

    O conserto:

     public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } }); public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } catch (Exception e) { public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } }); public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } } public void testCallBack() throws Throwable { final CountDownLatch signal = new CountDownLatch(1); // Execute the async task on the UI thread! THIS IS KEY! runTestOnUiThread(new Runnable() { @Oviewride public void run() { try { GetBusinessRulesTask task = (GetBusinessRulesTask)new GetBusinessRulesTask(new GetBusinessRulesTask.Receiview<BusinessRules>() { @Oviewride public void onReceiveResult( BusinessRules rules, Exception e) { assertNotNull(rules); assertNull(e); signal.countDown();// notify the count downlatch } }); task.stairt(mActivity.getApplicationContext()); } catch (Exception e) { Log.e(TAG, "ERROR", e); fail(); } } }); try { signal.await();// wait for callback } catch (InterruptedException e1) { fail(); e1.printStackTrace(); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.