简体   繁体   中英

Use Robolectric to test Activity with Loader

I have an Activity with a Spinner that uses a Loader to fetch data from the ContentProvider (as advised in several sources and as I've done before):

protected void onCreate(Bundle savedInstanceState) {

  // ...

  account = (Spinner) findViewById(R.id.account);
  account.setAdapter(
    new SimpleCursorAdapter(
      this,
      android.R.layout.simple_spinner_item,
      null,
      new String[]{ Contract.Accounts.COL_NAME },
      new int[]{ android.R.id.text1 },
      0));

  getLoaderManager().initLoader(LOADER_ID_ACCOUNT, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  return new CursorLoader(this,
    Contract.Accounts.CONTENT_URI,
    null,
    null,
    null,
    null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  ((SimpleCursorAdapter)account.getAdapter()).swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
  ((SimpleCursorAdapter)account.getAdapter()).swapCursor(null);
}

So far so good, the app works like a charm when I run it, but the unit tests I had for this Activity won't run now.

@Test
public void testAllElementsExist() {
  Activity activity = setupActivity(AddTransactionActivity.class);

  assertTrue(activity.findViewById(R.id.category) != null);
  assertTrue(activity.findViewById(R.id.created_on_date) != null);
  assertTrue(activity.findViewById(R.id.created_on_time) != null);
  assertTrue(activity.findViewById(R.id.account) != null);
  assertTrue(activity.findViewById(R.id.description) != null);
}

Even the most basic test fails:

java.lang.NullPointerException
at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:328)
at android.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:345)
at pt.lemonade.AddTransactionActivity.onLoadFinished(AddTransactionActivity.java:185)
at pt.lemonade.AddTransactionActivity.onLoadFinished(AddTransactionActivity.java:28)
at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:482)
at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:450)
at android.content.Loader.deliverResult(Loader.java:143)
at android.content.CursorLoader.deliverResult(CursorLoader.java:113)
at android.content.CursorLoader.deliverResult(CursorLoader.java:43)
at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:254)
at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:91)
at android.os.ShadowAsyncTaskBridge.onPostExecute(ShadowAsyncTaskBridge.java:22)
at org.robolectric.shadows.ShadowAsyncTask$1$1.run(ShadowAsyncTask.java:40)
at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:182)
at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:125)
at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:110)
at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:86)
at org.robolectric.util.Scheduler.unPause(Scheduler.java:26)
at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:231)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:270)
at org.robolectric.util.ActivityController.visible(ActivityController.java:166)
at org.robolectric.util.ActivityController.setup(ActivityController.java:202)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:1388)
at pt.lemonade.AddTransactionActivityTest.testAllElementsExist(AddTransactionActivityTest.java:65)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Is this a known issue of Robolectric 2.4? Is there a workaround that doesn't translate into writing a bunch of shadow classes?

Tools of trade: Android Studio 1.1.0 with Unit Testing experimental feature activated, Gradle plugin 1.0.1, Robolectric 2.4

Also, some aside questions: I've recently heard that it's best to use RxJava Observables because it's easier to test. Can it be used as a substitute to the LoaderManager? How can I cache the results as the LoaderManager does? Can you give me a working example of a query to the ContentProvider and a unit test so I can evaluate?

In case anyone falls into the same issue: Robolectric lacked ShadowSimpleCursorAdapter#swapCursor implementation. I've added that method along with other modifications and created a pull request. You may find it in the master branch. As for anyone still using version 2.4, check this out: https://github.com/robolectric/robolectric/issues/1677

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM