Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittesting AsyncTaskLoader with getLoaderResultSynchronously

I am trying to create unit tests for a REST client that does some API calls. The client works fine in the live application, but I can't get it to run in the test case.

Apparantly, LoaderTestCase.getLoaderResultSynchronously() could be used here (at least according to Android reference, but it will not accept my loader. The code:

public void testGetEventInfo() {
    // Init some vars
    ...

    // Create & execute loader
    RESTLoader loader = new RESTLoader(getContext(),
            RESTLoader.HTTPVerb.GET, action, params, LOADER_GET_NEWS);
    getLoaderResultSynchronously(loader);
}

This yields the error getLoaderResultSynchronously(Loader) in the type LoaderTestCase is not applicable for the arguments (RESTLoader).

RESTLoader extends AsyncLoader. Note that I'm using the supportlibrary, maybe the Loader in there is incompatible? The documentation gives no information on this.

I've tried to solve this in several ways, though none seem to work:

  • Registered a listener to loader. However, the callback never triggers
  • Using CountdownLatch (also with a listener). Again, no trigger/countdown timeout.
  • Playing around with the type template (), without success.
  • Similar solutions on SO, though again failing to reach the listener.

Does anybody know why getLoaderResultSynchronously will not accept the loader? Or another clean way of testing the Loader, including a way to test return data? I can test handling the return data in a separate case, but I would also like to test the actual data.

Sincerely,

like image 507
M. Meiboom Avatar asked Jul 06 '12 11:07

M. Meiboom


1 Answers

Have you taken a look at the source code? You'll find the following import statements:

import android.content.Loader;
import android.content.Loader.OnLoadCompleteListener;

It doesn't look like Android offers a support version for LoaderTestCase. The easiest solution would be to temporarily change to the non-support LoaderManager (that is, have your class make use of the android.content.Loader instead), test your app, and then switch back to the support implementation. You might also consider copying the testing source code into your project, import the support LoaderManager, and execute it directly. I'm not familiar with the test libraries for Loaders but it doesn't seem outwardly obvious that this would cause any major issues.

like image 52
Alex Lockwood Avatar answered Sep 20 '22 13:09

Alex Lockwood