これは、私にとっては問題なく機能した Robolectric (v2.4) テストです。
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);
}
}