鉴于我正在开发一个简单的 ListFragment(在本例中,它从 MediaStore 读取艺术家列表,但稍后还会从不同来源读取数据),如下所示:
@EFragment
public class ArtistsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = ArtistsFragment.class.getName();
private SimpleCursorAdapter mAdapter;
Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
CursorLoader mCursorLoader;
@AfterViews
void setupView() {
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[]{MediaStore.Audio.Artists.ARTIST}, // lists path of files
new int[]{android.R.id.text1}, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (mCursorLoader == null) {
mCursorLoader = new CursorLoader(getActivity(), uri, new String[]{MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST},
null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
} else {
System.out.println("mCursorLoader.count: " + mCursorLoader.loadInBackground().getCount());
}
return mCursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
setListShown(true);
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
我想使用Robolectric + Mockito + awaitility来证明 Fragment 在各种条件下(例如空列表或无效数据等)的行为正常。我的测试类如下所示:
@RunWith(RobolectricTestRunner.class)
public class ArtistsFragmentTest {
@Test
public void shouldNotBeNull() {
final ArtistsFragment myFragment = ArtistsFragment_.builder().build();
assertNotNull(myFragment);
// Create a mock cursor.
final Cursor mc = getSampleArtistCursor();
when(mc.getCount()).thenReturn(1);
when(mc.getInt(0)).thenReturn(1);
when(mc.getString(1)).thenReturn("Sample Title");
myFragment.mCursorLoader = mock(CursorLoader.class);
when(myFragment.mCursorLoader.loadInBackground()).thenReturn(mc);
startFragment(myFragment);
assertNotNull(myFragment.getListView());
await().atMost(5, TimeUnit.SECONDS).until(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return myFragment.getListAdapter().getCount();
}
}, equalTo(1));
System.out.println(myFragment.getListAdapter().getCount());
}
private Cursor getSampleArtistCursor() {
return new CursorWrapper(mock(MockCursor.class));
}
}
然后在 IntelliJ 或 maven 中运行此测试时,测试将失败,适配器将始终返回零计数。
但是 onCreateLoader 中的 System.out.println 语句返回1。我需要特别注意后台线程中的 Mockito吗?(loadInBackground 方法在工作线程上运行)。