【发布时间】:2013-12-03 08:47:26
【问题描述】:
有人知道如何使用 Robolectric 测试以下设置吗?
包含 ViewPager 的片段,使用 CursorLoader 加载的数据。
使用下面的代码,CursorLoader 永远不会被推送到视图分页器的适配器中。
我在await() 通话中卡住了。
EventsFragmentTest.java:
@RunWith(CustomRobolectricTestRunner.class)
public class EventsFragmentTest extends AbstractDbAndUiDriver
{
// which element in the view pager we are testing
private static final int TEST_INDEX = 0;
protected SherlockFragmentActivity mActivity;
protected EventsFragment_ mFragment;
@Override
@Before
public void setUp() throws Exception
{
// create activity to hold the fragment
this.mActivity = CustomRobolectricTestRunner.getActivity();
// create and start the fragment
this.mFragment = new EventsFragment_();
}
@Test
public void sanityTest()
{
// create an event
final Event event = this.createEvent();
// create mock cursor loader
final Cursor cursor = this.createMockEventCursor(event);
this.mFragment.setCursorLoader(mock(CursorLoader.class));
when(this.mFragment.getCursorLoader().loadInBackground()).thenReturn(cursor);
CustomRobolectricTestRunner.startFragment(this.mActivity, this.mFragment);
await().atMost(5, SECONDS).until(this.isCursorLoaderLoaded(), equalTo(true));
// check for data displayed
final TextView title = this.getTextView(R.id.event_view_title);
final TextView text = this.getTextView(R.id.event_view_text);
// exists and visible is enough for now
this.getImageView(R.id.event_view_image);
assertThat(title.getText().toString(), equalTo(event.getTitle()));
assertThat(text.getText().toString(), is(event.getText()));
// clean up
cursor.close();
}
@Override
protected View getRootView()
{
return ((ViewPager) this.mFragment.getView().findViewById(R.id.events_pager)).getChildAt(TEST_INDEX);
}
private Callable<Boolean> isCursorLoaderLoaded()
{
return new Callable<Boolean>()
{
public Boolean call() throws Exception
{
return EventsFragmentTest.this.mFragment.isCursorLoaderLoaded(); // The condition that must be fulfilled
}
};
}
/**
* Create an event
*
* @return
*/
protected Event createEvent()
{
// create a random event
final Event event = new Event();
event.setImage(null);
event.setLink("/some/link/" + RandomUtils.getRandomString(5)); //$NON-NLS-1$
event.setResourceUri("/rest/uri/" + RandomUtils.getRandomDouble()); //$NON-NLS-1$
event.setText("this is a test object " + RandomUtils.getRandomString(5)); //$NON-NLS-1$
return event;
}
protected Cursor createMockEventCursor(final Event event)
{
// Create a mock cursor.
final Cursor cursor = new CursorWrapper(mock(MockCursor.class));
when(cursor.getCount()).thenReturn(1);
when(cursor.getString(cursor.getColumnIndexOrThrow(EventTable.COLUMN_TEXT))).thenReturn(event.getText());
when(cursor.getString(cursor.getColumnIndexOrThrow(EventTable.COLUMN_TITLE))).thenReturn(event.getTitle());
when(cursor.getString(cursor.getColumnIndexOrThrow(EventTable.COLUMN_IMAGE))).thenReturn(event.getImage());
when(cursor.getString(cursor.getColumnIndexOrThrow(EventTable.COLUMN_LINK))).thenReturn(event.getLink());
when(cursor.getString(cursor.getColumnIndexOrThrow(EventTable.COLUMN_RESOURCE_URI))).thenReturn(
event.getResourceUri());
// return created event
return cursor;
}
}
EventsFragment.java
@EFragment(resName = "events_fragment")
public class EventsFragment extends SherlockFragment implements LoaderCallbacks<Cursor>
{
@ViewById(R.id.events_pager)
protected ViewPager mPager;
@ViewById(R.id.events_indicator)
protected CirclePageIndicator mIndicator;
@Pref
protected ISharedPrefs_ mPreferences;
protected EventsFragmentAdapter pageAdapter;
private CursorLoader mCursorLoader;
/**
* initialise the cursoradapter and the cursor loader manager.
*/
@AfterViews
void init()
{
final SherlockFragmentActivity activity = this.getSherlockActivity();
this.pageAdapter = new EventsFragmentAdapter(activity.getSupportFragmentManager(), null);
this.mPager.setAdapter(this.pageAdapter);
this.mIndicator.setViewPager(this.mPager);
this.getLoaderManager().initLoader(this.mPager.getId(), null, this);
}
/* (non-Javadoc)
* @see android.support.v4.app.LoaderManager.LoaderCallbacks#onCreateLoader(int, android.os.Bundle)
*/
@Override
public Loader<Cursor> onCreateLoader(final int arg0, final Bundle arg1)
{
if (this.mCursorLoader == null)
{
// set sort to newest first
final String sortOrder = BaseColumns._ID + " DESC"; //$NON-NLS-1$
this.mCursorLoader = new CursorLoader(this.getActivity(), EventContentProvider.CONTENT_URI,
EventTable.getProjection(), AbstractDbTable.getWhereCondition(null),
AbstractDbTable.getWhereArgs(this.mPreferences, null), sortOrder);
}
return this.mCursorLoader;
}
/* (non-Javadoc)
* @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoadFinished(android.support.v4.content.Loader, java.lang.Object)
*/
@Override
public void onLoadFinished(final Loader<Cursor> arg0, final Cursor cursor)
{
this.pageAdapter.swapCursor(cursor);
}
/* (non-Javadoc)
* @see android.support.v4.app.LoaderManager.LoaderCallbacks#onLoaderReset(android.support.v4.content.Loader)
*/
@Override
public void onLoaderReset(final Loader<Cursor> arg0)
{
this.pageAdapter.swapCursor(null);
}
/**
* Required for testing only.
*
* @param cursorLoader
*/
public void setCursorLoader(final CursorLoader cursorLoader)
{
this.mCursorLoader = cursorLoader;
}
/**
* Required for testing only.
*
* @param cursorLoader
*/
public CursorLoader getCursorLoader()
{
return this.mCursorLoader;
}
public boolean isCursorLoaderLoaded()
{
return (this.pageAdapter.getCursor() != null);
}
}
【问题讨论】:
-
看起来您正在尝试编写更多系统测试而不是单元测试,这是一个正确的假设吗?如果是这样,使用 Robotium 或 Espresso 可以更轻松地完成。当您尝试编写具有 Android 特定参考但不测试用户将看到的内容的单元或集成测试时,Robolectric 非常有用。
-
我有点想念,Robolectric 正在尝试帮助您测试用户所看到的内容。我认为您将遇到的问题是您必须编写太多引导代码,这些代码可能无法在生产中测试正确的代码路径,并且尝试使用定时等待通常会使测试变得脆弱,因为不知道如何需要很长的异步任务。
-
我正在尝试进行快速测试。我可以并且经常在 Robotium 中编写测试,但它们的运行速度比 Robolectric 慢得多。对我来说,理想的世界是没有设置、支持和维护两组测试。这是我的意图。
-
只是一个想法,
Robolectric.runBackgroundTasks();任何好处 - 可能代替await -
我同意@Elliott 的观点,您正在尝试创建集成测试或单元测试等场景测试,如果您想破解\滥用robolectric,这不是它的意思,但这是有代价的。 robolectric 仅提供存根,不提供类。
标签: android robolectric android-testing android-cursorloader