Very Basic LoaderTestCase stuck in getLoaderResultSynchronously

431 views Asked by At

I am new to Loaders and am trying to test the most basic AsyncTaskLoader in a LoaderTestCase. However getLoaderResultSynchronously never finishes. This ist the whole class:

public class LoaderTest extends LoaderTestCase{

    public void testLoader() {
        String result = getLoaderResultSynchronously(new AsyncTaskLoader<String>(getContext()){

            @Override
            public String loadInBackground() {
                return "hello";
            }

        });

        assertEquals(result, "hello");
    }
}

What am I missing?

1

There are 1 answers

0
Davide On

The getLoaderResultSynchronously() call startLoading() and stopLoading() methods of the Loader passed by parameter. You must implement it.

public final void testLoader() {
    String result = loader.getLoaderResultSynchronously(new AsyncTaskLoader<String>(
            getContext()) {

        @Override
        public String loadInBackground() {
            return "hello";
        }

        /* (non-Javadoc)
         * @see android.content.Loader#onStartLoading()
         */
        @Override
        protected void onStartLoading() {
            // TODO Auto-generated method stub
            forceLoad();
        }
        /* (non-Javadoc)
         * @see android.content.Loader#onStopLoading()
         */

        @Override
        protected void onStopLoading() {
            // TODO Auto-generated method stub
            cancelLoad();

        }



    });