Tried ProviderTestRule many times to test my custom ContentProvider class but succeeded only when add my Test class to androidTest folder, here is the example kotlin code :
//this is my custom provider class
public final class NameProvider : ContentProvider() {
private var pContext: Context? = null;
private var nameDbHelper: NameDbHelper? = null
private var dataBase: SQLiteDatabase? = null
private val Log_Tag = "NAME_PROVIDER"
override fun onCreate(): Boolean {
pContext = context
nameDbHelper = pContext?.let { NameDbHelper(it) }
dataBase = nameDbHelper?.writableDatabase
if (dataBase == null) {
Log.d(Log_Tag, "dataBase NOT created ...")
return false
}
Log.d(Log_Tag, "dataBase created ...")
return true
}
override fun query(p0: Uri, p1: Array<out String>?, p2: String?, p3: Array<out String>?, p4: String?): Cursor? {
var queryBuilder : SQLiteQueryBuilder = SQLiteQueryBuilder()
queryBuilder.tables = "TABLE_NAME"
val cursor = queryBuilder.query(dataBase,p1,p2,p3,null,null, "COLUMN_NAME")
cursor.setNotificationUri(pContext?.contentResolver, p0)
return cursor
}
override fun insert(p0: Uri, p1: ContentValues?): Uri? {
val rowID: Long? = dataBase?.insert("TABLE_NAME", "", p1)
rowID?.let {
if (it > 0) {
val _uri = ContentUris.withAppendedId(CONTENT_URI, it)
pContext?.contentResolver?.notifyChange(_uri, null)
return _uri
}
}
throw SQLException("Failed to add a record into")
}
override fun getType(p0: Uri): String? {
TODO("Not yet implemented")
}
override fun delete(p0: Uri, p1: String?, p2: Array<out String>?): Int {
TODO("Not yet implemented")
}
override fun update(p0: Uri, p1: ContentValues?, p2: String?, p3: Array<out String>?): Int {
TODO("Not yet implemented")
}
}
Now the test class that I can run successfully :
@RunWith(AndroidJUnit4::class)
class NameProviderTest {
@get:Rule
var mProviderRule = ProviderTestRule.Builder(NameProvider::class.java, NameContract.AUTHORITY)
.setDatabaseCommands(NameContract.DATABASE_NAME).build()
@Test
fun contentResolverNull(){
val resolver = mProviderRule.resolver
assertNotNull(resolver)
}
@Test
fun query() {
val cursor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mProviderRule.resolver.query(NameContract.CONTENT_URI, null, null, null)
} else {
null
}
assertNotNull(cursor)
var name : String? = null
if(cursor != null && cursor.moveToFirst()){
name = cursor.getString(cursor.getColumnIndex(NameContract.KEY_NAME))
}
assertNotNull(name)
assertEquals(name, "Taher")
}
@Test
fun insert() {
val values = ContentValues()
values.put(NameContract.KEY_NAME, "Taher");
val uri = mProviderRule.resolver.insert(NameContract.CONTENT_URI, values);
assertNotNull(uri)
}
}
Better Approach
But then I have found a better way to do this by using robolectric:4.2 . This time it feels like actual unit test as I can do this by adding my Test class NameProviderTest to the test folder.
Now the test class look like this:
@Config(maxSdk = 29)
@RunWith(RobolectricTestRunner::class)
class NameProviderTest {
private var contentResolver : ContentResolver? = null
private var shadowContentResolver : ShadowContentResolver? = null
private var nameProvider : NameProvider? = null
@Before
fun setUp() {
contentResolver = ApplicationProvider.getApplicationContext<Context>().contentResolver
val providerInfo : ProviderInfo = ProviderInfo()
providerInfo.authority = NameContract.AUTHORITY
providerInfo.grantUriPermissions = true
val controller = Robolectric.buildContentProvider(NameProvider::class.java).create(providerInfo)
shadowContentResolver = shadowOf(contentResolver)
nameProvider = controller.get()
}
@Test
fun onCreate() {
val res = nameProvider?.onCreate()
assertEquals(res, true)
//ShadowContentResolver.registerProviderInternal(NameContract.AUTHORITY, nameProvider)
}
@Test
fun query() {
val values = ContentValues()
values.put(NameContract.KEY_NAME, "ABCD")
val uri = contentResolver?.insert(NameContract.CONTENT_URI, values);
assertNotNull(uri)
val cursor = contentResolver?.query(NameContract.CONTENT_URI,null,null,null,null)
assertNotNull(cursor)
var name : String? = null
if(cursor != null && cursor.moveToFirst()){
name = cursor.getString(cursor.getColumnIndex(NameContract.KEY_NAME))
}
cursor?.close()
assertNotNull(name)
assertEquals(name, "ABCD")
}
@Test
fun insert() {
val values = ContentValues()
values.put(NameContract.KEY_NAME, "Taher")
val uri = contentResolver?.insert(NameContract.CONTENT_URI, values);
assertNotNull(uri)
}
}