感谢Wim Deblauwe的回答,我也完成了Kotlin中的roboelectric 4.2
,这是完整的演示:
这是我的自定义提供程序类
//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")
}
}
这是我的机器人电动测试课
@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)
}
}