ProviderTestCase2在getMockContentResolver()上返回null

时间:2015-12-02 22:39:47

标签: android android-testing

我正在使用ProviderTestCase2来测试我的ContentProvider,LinesCP,但是调用getMockContentResolver()会返回null。

LinesCP的类路径是'com.olfybsppa.inglesaventurero / start / LinesCP.java'。这是显示AUTHORITY的代码段。

public class LinesCP extends ContentProvider {

  private static final String database_name = "lines.db";

  public static final String AUTHORITY = "com.olfybsppa.inglesaventurero.start.LinesCP";

我正在使用BuildVariants Unit Test运行它,我的其他单元测试工作。这是不起作用的测试。

public class ContentProviderUTest extends ProviderTestCase2<LinesCP>   {

  private static String ALPHABET = "AL-00";
  private MockContentResolver cr;

  public ContentProviderUTest () {
    super(LinesCP.class, "com.olfybsppa.inglesaventurero.start");
    //super(LinesCP.class, LinesCP.AUTHORITY);
  }

  public ContentProviderUTest (Class<LinesCP> providerClass, String providerAuthority) {
    super(providerClass, providerAuthority);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    cr = getMockContentResolver(); // cr is null.
  }

  public void testSceneInsert() {
    Uri uri = LinesCP.sceneTableUri;
    ContentValues sceneValues = new ContentValues();
    sceneValues.put(LinesCP.scene_id, ALPHABET);
    sceneValues.put(LinesCP.english_title, "Alphabet");
    sceneValues.put(LinesCP.spanish_title, "Alphabeto");
    MockContentResolver cr = getMockContentResolver();
    Uri resultingUri = getMockContentResolver().insert(uri,   sceneValues);
    assertNotNull(resultingUri);
    long rowId = ContentUris.parseId(resultingUri);
    assertTrue(rowId > 0);
  }

}

我认为这应该有效,欢迎任何想法。

1 个答案:

答案 0 :(得分:0)

快速定位:我的ContentProvider名为LinesCP。 LinesCP拥有一个CPHints表。 CPHint是我自己的课程。我的测试是ProviderInsertHintsTest,它扩展了ProviderTestCase2

即使ProviderTestCase2没有从InstrumentationTestCase继承,我也使用Build Variants的Android Instrumentation Tests运行ProviderInsertHintsTest。 ProviderInsertHintsTest位于我的src / androidTest / java /文件夹中。所以,我之前认为ProviderTestCase2应该与BuildVariants的“单元测试”一起运行,我认为那是我的主要错误。

在ProviderInsertHintsTest中,我测试了LineCP的insert(Uri,ContentValues)方法。我在setUp()中得到了mMockResolver。我使用mMockResolver插入一个由hint1制作的ContentValues。然后我使用mMockResolver.query从Content Provider中获取'fromCP'。然后我断言原来的'hint1'等于'fromCP'。

public class ProviderInsertHintsTest extends ProviderTestCase2<LinesCP> {

  private MockContentResolver mMockResolver;

  private CPHint hint1;
  private CPHint hint2;

  public ProviderInsertHintsTest() {
    super(LinesCP.class, LinesCP.AUTHORITY);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    mMockResolver = getMockContentResolver();

    hint1 = new CPHint(1);
    hint1.setNormalStartTime(1001);
    hint1.setNormalEndTime(1010);

    hint2 = new CPHint(2);
    hint2.setNormalStartTime(2001);
    hint2.setNormalEndTime(2010);
  }

  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
  }

  public void testInsertHint () {
    ContentValues cv = hint1.getContentValues(111);
    mMockResolver.insert(LinesCP.hintTableUri, cv);
    Cursor cursor = mMockResolver.query(LinesCP.hintTableUri, null, null, null, null);
    cursor.moveToFirst();
    assertEquals(1, cursor.getCount());
    CPHint fromCP = CPHint.extractCPHint(cursor);
    cursor.close();

    assertTrue(fromCP.equals(hint1));
  }

  public void testInsertTwoHintsDeleteOne () {
    ContentValues cv1 = hint1.getContentValues(111);
    ContentValues cv2 = hint2.getContentValues(111);

    mMockResolver.insert(LinesCP.hintTableUri, cv1);
    mMockResolver.insert(LinesCP.hintTableUri, cv2);

    Cursor cursor1 = mMockResolver.query(LinesCP.hintTableUri, null, null, null, null);
    assertEquals(2, cursor1.getCount());
    cursor1.close();

    mMockResolver.delete(LinesCP.hintTableUri, Ez.where(BaseColumns._ID, "" + 1), null);
    Cursor cursor2 = mMockResolver.query(LinesCP.hintTableUri, null, null, null, null);
    assertEquals(1, cursor2.getCount());
    cursor2.close();
  }
}

信仰的唯一飞跃是CPHint.extractCPHint(光标光标)。它只是为了干掉代码。它询问LineCP的列索引与列名匹配的值是什么,然后创建一个新的CPHint。在这里,以防万一。

  public static CPHint extractCPHint(Cursor cursor) {
    Integer position = cursor.getInt(cursor.getColumnIndex(LinesCP.pos_id));
    CPHint hint = new CPHint(position);
    hint.setTimes(cursor.getLong(cursor.getColumnIndex(LinesCP.normal_start_time)),
      cursor.getLong(cursor.getColumnIndex(LinesCP.normal_end_time)),
    return hint;
  }

要测试删除,我使用mMockResolver并将hint1和hint2添加到ContentProvider。然后我删除匹配BaseColumns.ID等于1的CPHint。然后我再次查询Content Provider并断言它只有一行,就像之前有两行一样。