ContentResolver查询在单元测试中返回null游标

时间:2017-08-02 08:41:29

标签: android unit-testing robolectric

我尝试在Android库中对我的数据库进行单元测试,这是我的工作方式。提供者在Manifest中被定义。我用RobolectricTestRunner

运行它
    @RunWith(CustomRobolectricTestRunner.class)
public class FileDatabaseTest {

  private ContentResolver contentResolver;

  @Before
  public void setUp() {
    Robolectric.buildContentProvider(FileIndexContentProvider.class);
    contentResolver = RuntimeEnvironment.application.getContentResolver();


    FileDatabase fileDatabase = mock(FileDatabase.class);

    Mockito.doAnswer(new Answer() {
      public Object answer(InvocationOnMock invocation) {
        return null;
      }
    }).when(fileDatabase).close();
  }

  public void insertFileIndex() {
    ContentValues contentValues = new ContentValues();
    contentValues.put(FileContract.FileIndexTable.FILE_PATH, "path");
    contentValues.put(FileContract.FileIndexTable.FILENAME, "filename");
    contentValues.put(FileContract.FileIndexTable.STATUS, FileIndexEntry.FileStatus.ACTIVE.toString());
    contentValues.put(FileContract.FileIndexTable.CREATE_DATE, System.currentTimeMillis());
    contentResolver.insert(FileContract.FileIndexTable.CONTENT_URI, contentValues);
  }

  @Test
  public void testInsertFile() {
    insertFileIndex();
    Cursor cursor = contentResolver.query(FileContract.FileIndexTable.CONTENT_URI,
        FileContract.FileIndexTable.PROJECTION, null, null, null, null);
    assertNotNull(cursor);
    assertEquals(cursor.getCount(), 1);
    cursor.moveToFirst();
    FileIndexEntry fileIndexEntry = new FileIndexEntry(cursor);
    assertThat(fileIndexEntry.filename, equalTo("filename"));
  }
}

查询返回的游标为null。在所有适用于db的测试中都会发生这种情况。

public class FileIndexContentProvider extends ContentProviderBase {

  private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

  private static final int FILE_INDEX = 100;
  private static final int FILE_INDEX_ID = 101;

  static {
    URI_MATCHER.addURI(FileContract.AUTHORITY, FileIndexTable.TABLE_NAME, FILE_INDEX);
    URI_MATCHER.addURI(FileContract.AUTHORITY, FileIndexTable.TABLE_NAME + "/*", FILE_INDEX_ID);
  }

  /**
   * Static method to obtain table name from a Uri.
   */
  public static String tableForUri(Uri uri) {
    final int match = URI_MATCHER.match(uri);
    switch (match) {
      case FILE_INDEX:
      case FILE_INDEX_ID:
        return FileIndexTable.TABLE_NAME;
      default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
  }

  @Override
  public boolean onCreate() {
    databaseHelper = new FileDatabase(getContext());
    return true;
  }

  @Override
  public String getTableForUri(Uri uri) {
    return tableForUri(uri);
  }

  @Override
  public String getType(@NonNull Uri uri) {
    final int match = URI_MATCHER.match(uri);
    switch (match) {
      case FILE_INDEX:
        return FileIndexTable.CONTENT_TYPE;
      case FILE_INDEX_ID:
        return FileIndexTable.CONTENT_ITEM_TYPE;
      default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }
  }
}

这是ContentProvider代码。它在应用程序中测试得很好,但是当我开始将其移入库中时,问题就开始了。

0 个答案:

没有答案