使用Robolectric来测试带有Loader的Activity

时间:2015-04-14 10:35:56

标签: java android unit-testing robolectric rx-java

我有一个带有Spinner的Activity,它使用Loader从ContentProvider中获取数据(正如我以前在几个来源中所建议的那样):

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);
}

到目前为止,这个应用程序在我运行时就像一个魅力,但我对此活动的单元测试现在不会运行。

@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);
}

即使是最基本的测试也失败了:

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)

这是Robolectric 2.4的已知问题吗?是否有一种解决方法无法转化为编写一组影子类?

交易工具:启用了单元测试实验功能的Android Studio 1.1.0,Gradle插件1.0.1,Robolectric 2.4

此外,还有一些问题:我最近听说最好使用RxJava Observables,因为它更容易测试。它可以用作LoaderManager的替代品吗?如何像LoaderManager一样缓存结果?你能给我一个关于ContentProvider查询和单元测试的工作示例,以便我可以评估吗?

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题: Robolectric缺乏ShadowSimpleCursorAdapter#swapCursor实施。我已经添加了该方法以及其他修改并创建了拉取请求。您可以在主分支中找到它。对于仍在使用2.4版本的用户,请查看:https://github.com/robolectric/robolectric/issues/1677