@Test public void getCategoryId() { when(database.query("category", null, "name=?", new String[] {"test"}, null, null, null)) .thenReturn(cursor); when(cursor.getCount()).thenReturn(1); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getLong(anyInt())).thenReturn(2L); assertEquals(Long.valueOf(2L), source.getCategoryId("test")); }
@VisibleForTesting Long getCategoryId(String name) { Cursor cursor = database.query( CategoryModel.TABLE, null, CategoryModel.COLUMN_NAME + "=?", new String[] {name}, null, null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(CategoryModel.COLUMN_ID)); cursor.close(); return id; } else { return null; } }
DataSource { @VisibleForTesting Long getCategoryId(String name) { Cursor cursor = database.query( CategoryModel.TABLE, null, CategoryModel.COLUMN_NAME + "=?", new String[] {name}, null, null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(CategoryModel.COLUMN_ID)); cursor.close(); return id; } else { return null; } } }
DataSource { @VisibleForTesting Long getCategoryId(String name) { Cursor cursor = database.query( CategoryModel.TABLE, null, CategoryModel.COLUMN_NAME + "=?", new String[] {name}, null, null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(CategoryModel.COLUMN_ID)); cursor.close(); return id; } else { return null; } } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); }
DataSource { @VisibleForTesting Long getCategoryId(String name) { Cursor cursor = database.query( CategoryModel.TABLE, null, CategoryModel.COLUMN_NAME + "=?", new String[] {name}, null, null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(CategoryModel.COLUMN_ID)); cursor.close(); return id; } else { return null; } } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); static DataSource get(Context context); synchronized void open(); boolean isOpen(); synchronized void close(); @VisibleForTesting SQLiteDatabase getDatabase(); void clearTables(); void beginTransaction(); void execSql(String sql); Cursor rawQuery(String sql); void setTransactionSuccessful(); void endTransaction(); long insertArticle(Article article); int updateSavedArticleState(Article article); int updateArticleInsertedAtTimestamp(Article article); void updateArticleContent(Article article); Article getArticle(String url); Cursor getAllArticles(); Cursor getSavedArticles(); Cursor getArticlesForSource(long remoteSourceId); void deleteArticle(Article article); void deleteArticle(long articleId); long insertCategory(String categoryName); void insertSource(Source source); void deleteSource(String name); List<Source> getSources(); Source getSource(long remoteId); CategoryCounts getCategoryCounts(long timestamp); }
DataSource { @VisibleForTesting Long getCategoryId(String name) { Cursor cursor = database.query( CategoryModel.TABLE, null, CategoryModel.COLUMN_NAME + "=?", new String[] {name}, null, null, null); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(CategoryModel.COLUMN_ID)); cursor.close(); return id; } else { return null; } } private  DataSource(Context context); @VisibleForTesting  DataSource(DatabaseSQLiteHelper helper); @VisibleForTesting  DataSource(SQLiteDatabase database); static DataSource get(Context context); synchronized void open(); boolean isOpen(); synchronized void close(); @VisibleForTesting SQLiteDatabase getDatabase(); void clearTables(); void beginTransaction(); void execSql(String sql); Cursor rawQuery(String sql); void setTransactionSuccessful(); void endTransaction(); long insertArticle(Article article); int updateSavedArticleState(Article article); int updateArticleInsertedAtTimestamp(Article article); void updateArticleContent(Article article); Article getArticle(String url); Cursor getAllArticles(); Cursor getSavedArticles(); Cursor getArticlesForSource(long remoteSourceId); void deleteArticle(Article article); void deleteArticle(long articleId); long insertCategory(String categoryName); void insertSource(Source source); void deleteSource(String name); List<Source> getSources(); Source getSource(long remoteId); CategoryCounts getCategoryCounts(long timestamp);  }