how to test insert/query/delete/update of a custom ContentProvider ?

2,433 views
Skip to first unread message

Gal Ben-Haim

unread,
Aug 20, 2012, 3:08:36 AM8/20/12
to robol...@googlegroups.com
I understand that for query I'm supposed to create a TestCursor with some mock data but how do I test the other methods ?
(for example, I want to test that insert returns the correct URL)

Jan Berkel

unread,
Aug 20, 2012, 4:45:56 AM8/20/12
to robol...@googlegroups.com


On Monday, August 20, 2012 9:08:36 AM UTC+2, Gal Ben-Haim wrote:
I understand that for query I'm supposed to create a TestCursor with some mock data but how do I test the other methods ?
(for example, I want to test that insert returns the correct URL)


here's how i integrate my contentprovider with the rest of the system. create a custom test runner and override beforeTest

@Override public void beforeTest(Method method) {
  ContentProvider provider = new MyCustomContentProvider();
  provider.onCreate();
  ShadowContentResolver.registerProvider(authority, provider);
}

you should be able to access your content provider via normal content resolver calls. if you use sqlite as backing store it should just work.
if not, you might want to mock out your storage layer.

   jan




Gal Ben-Haim

unread,
Aug 20, 2012, 5:02:23 AM8/20/12
to robol...@googlegroups.com
this is what I'm trying to do, for some reason the returned Cursor from the query operation is null.

what am I missing ?


private Activity mContext;
private CustomContentProvider mProvider;
private ContentResolver mResolver;


@Before
public void setUp() throws Exception {
mProvider = new CustomContentProvider();
mContext = new Activity();
mResolver = mContext.getContentResolver();
mProvider.onCreate();
ShadowContentResolver.registerProvider(ProviderMetaData.AUTHORITY, mProvider);
}

@Test
public void testInsertUsers() {
Uri uri = mResolver.insert(ProviderMetaData.USERS_URI, DatabaseTestObjects.getUserObject(1));
// check if the correct uri is returned
assertEquals(uri, Uri.withAppendedPath(ProviderMetaData.USERS_URI, Long.toString(1)));
// check if the data was inserted
Cursor c = mResolver.query(ProviderMetaData.USERS_URI, null, null, null, null);
assertNotNull(c);
assertEquals(1, c.getCount());
}

Jan Berkel

unread,
Aug 20, 2012, 5:13:56 AM8/20/12
to robol...@googlegroups.com


@Test
public void testInsertUsers() {
Uri uri = mResolver.insert(ProviderMetaData.USERS_URI, DatabaseTestObjects.getUserObject(1));
// check if the correct uri is returned
assertEquals(uri, Uri.withAppendedPath(ProviderMetaData.USERS_URI, Long.toString(1)));
// check if the data was inserted
Cursor c = mResolver.query(ProviderMetaData.USERS_URI, null, null, null, null);


this should probably be

Cursor c = mResolver.query(uri, null, null, null, null);

  jan

Gal Ben-Haim

unread,
Aug 20, 2012, 5:17:12 AM8/20/12
to robol...@googlegroups.com
why does query return a null Cursor and how do I fix it ?

Jan Berkel

unread,
Aug 20, 2012, 5:54:35 AM8/20/12
to robol...@googlegroups.com


On Monday, August 20, 2012 11:17:12 AM UTC+2, Gal Ben-Haim wrote:
why does query return a null Cursor and how do I fix it ?


are you sure your provider does the right thing? connect a debugger and check. don't think it's related to robolectric.


 

Gal Ben-Haim

unread,
Aug 20, 2012, 5:59:13 AM8/20/12
to robol...@googlegroups.com
this exact same test is passing with Android's testing framework so its not a problem with the provider.

what can be the problem ?

MikeTheBiker

unread,
Sep 10, 2012, 1:37:23 PM9/10/12
to robol...@googlegroups.com
I think you need something like:

@Before
public void setUp() {
...

mMockResolver = Robolectric.application.getContentResolver();

final ShadowContentResolver shadowContentResolver = shadowOf(mMockResolver);

cursor = new SimpleTestCursor();
shadowContentResolver.setCursor(YourContentProvider.URI, cursor);

...
}

Regards

Lei Yu

unread,
Jul 12, 2013, 5:56:05 PM7/12/13
to robol...@googlegroups.com
Hi, guys:
I just get started using Robolectric and writing some unit test on content provider. 
I follow the @Before setUp in this post, but it won't work.
An error will show up inside query function of content provider at line SQLiteDatabase db = myDB.getReadableDatabase();
However, it runs well on device. 
I know this post has been there for a while. Can anyone help me with this?

Many thanks.
Leo. 

Budhdi Prakash Sharma

unread,
May 24, 2017, 5:29:49 AM5/24/17
to Robolectric
Hey @Gal Ben-Haim   did you get the correct solution of this problem because i am also facing the same problem.

While returning the value from database Cursor giving null pointer exception.


@Test
    public void testQuery(){

        System.out.println(" Query URI :  " + CONTENT_URI);
        Cursor cursor = contentResolver.query(CONTENT_URI,null,null,null,null);
        //String[] Data=null;

        cursor.moveToFirst();

        if(cursor.getCount() != 0){
            do {
                for(int i=0;i<cursor.getCount();i++)
                {
                   Assert.assertNotEquals( projection[i],cursor.getString(i));
                }


            }while (cursor.moveToNext());
Reply all
Reply to author
Forward
0 new messages