I'm trying to mock the getter of a property with n...
# android
b
I'm trying to mock the getter of a property with no success.
Copy code
open class Preferences (context: Context) {
    val USER_ID = "USER_ID"
    val mSharedPreferences : SharedPreferences = context.getSharedPreferences("shared_prefs", Context.MODE_PRIVATE)

    var userId: String
        get() = mSharedPreferences.getString(USER_ID, "---------")
        set(value) = mSharedPreferences.edit().putString(USER_ID, value).apply()
}
Copy code
Mockito.`when`(mPrefs.userId).thenReturn("test-user-1")
or
Mockito.`when`(Preferences::class.java.getDeclaredMethod("getUserId").invoke(null, null)).thenReturn("test-user-1")
Null pointer exception getUserId. Any help?