I have noticed that every now and then there is a question about using Robolectric for testing custom ContentProviders. However, there has never been a concrete and unambiguous answer on how to do it properly. I have stumbled upon 2 different approaches:
one saying you can simply instantiate an in-memory ContentProvider, which you can use to insert and query data (https://gist.github.com/anonymous/6139359)
the other saying to use the ShadowContentResolver to set mock cursor data (https://groups.google.com/d/msg/robolectric/r35mMirIkTs/xJJBNXl_RgwJ)
However, I'm getting a java.lang.InstantiationException with both approaches. There have been some SO posts saying that this is due to SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java) not being overriden in Robolectric (Android + Robolectric - RuntimeException / InstantiationException in queryBuilder.query() in ContentProvider).
I guess my question is - are there any preffered workarounds that make testing ContentProviders possible. Or are there any other approaches that are better then those 2 mentioned above.
This is the Robolectric (v2.4) test that worked fine for me:
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowContentResolver;
import co.tomup.app.db.DbSchema;
import co.tomup.app.db.TomupContentProvider;
import co.tomup.app.db.model.CalendarDay;
import co.tomup.app.db.tables.CalendarDayTable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class CalendarDayProviderTest {
private ContentResolver mContentResolver;
private ShadowContentResolver mShadowContentResolver;
private TomupContentProvider mProvider;
@Before
public void setup() {
mProvider = new TomupContentProvider();
mContentResolver = Robolectric.application.getContentResolver();
mShadowContentResolver = Robolectric.shadowOf(mContentResolver);
mProvider.onCreate();
ShadowContentResolver.registerProvider(DbSchema.AUTHORITY, mProvider);
}
@Test
public void testInsertAndDelete() {
// insert
CalendarDay calendarDay = new CalendarDay();
calendarDay.setId(1L);
calendarDay.setDay(new LocalDate());
calendarDay.setMoonPhase("new");
calendarDay.setSunrise(1);
calendarDay.setSunset(100);
Uri insertionId = mContentResolver.insert(CalendarDayTable.CONTENT_URI,
calendarDay.toSQLiteContentValues());
Cursor cursorCheck = mShadowContentResolver.query(CalendarDayTable.CONTENT_URI,
null, null, null, null);
while (cursorCheck.moveToNext()) {
CalendarDay calendarDayCheck = CalendarDay.fromSQLiteCursor(cursorCheck);
assertEquals(calendarDay, calendarDayCheck);
}
assertTrue(cursorCheck.getCount() > 0);
// delete
mShadowContentResolver.delete(insertionId,
null, null);
cursorCheck = mShadowContentResolver.query(CalendarDayTable.CONTENT_URI,
null, null, null, null);
assertTrue(cursorCheck.getCount() == 0);
}
}