- Create a function that returns the SharedPreferences instance:
fun getDefaultSharedPreferences() = PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getContext())
- Save a sample string to the SharedPreferences instance:
@Test
fun testUserParcelization() {
val prefs = getDefaultSharedPreferences()
val userName: String = "Gonzo"
prefs.edit {
putString("user_name", userName)
}
}
- Verify whether the string was successfully saved:
@Test
fun testSharedPrefs() {
val prefs = getDefaultSharedPreferences()
val userName: String = "Gonzo"
prefs.edit {
putString("user_name", userName)
}
val DEFAULT_VALUE = "empty"
val fetchedUserName = prefs.getString("user_name",
DEFAULT_VALUE)
assertSame(userName, fetchedUserName)
}
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.