测试与Robolectric&放一个CursorLoader;的Mockito [英] Testing a CursorLoader with Robolectric & Mockito

查看:170
本文介绍了测试与Robolectric&放一个CursorLoader;的Mockito的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我正在开发一个简单的ListFragment(在这种情况下,它会读取从MediaStore艺术家的名单,同时也将读取后,一个不同的源数据)是这样的:

Given I'm developing a simple ListFragment (in this case, it reads a list of Artists from the MediaStore, but will also read data from a different source later) like this:

@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 以证明片段正确的行为在不同条件(如空列表或无效的数据等)。我的测试类看起来是这样的:

I want to use Robolectric + Mockito + awaitility to proof the Fragment behaves properly on various conditions (e.g. empty list or invalid data etc). My test class looks like this:

@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测试将失败时,适配器将始终返回计数为零。

Then when running this test in IntelliJ or maven the test will fail, the adapter will always return a count of zero.

在onCreateLoader中的System.out.println语句但是返回的 1 。我是否需要采取特殊照顾的Mockito在后台线程? (该loadInBackground方法运行在一个工作线程)。

The System.out.println statement in onCreateLoader however returns 1. Do I need to take special care for Mockito in background threads? (the loadInBackground method runs on a worker thread).

推荐答案

该解决方案是使用:

Robolectric.flushForegroundThreadScheduler();

(Robolectric 3.0)

(Robolectric 3.0)

这将立即运行所有任务,包括的 CursorLoader 的。

This will run all tasks immediately, including the CursorLoader.

这篇关于测试与Robolectric&放一个CursorLoader;的Mockito的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆