How to do it...

  1. Create a function that returns the SharedPreferences instance:
fun getDefaultSharedPreferences() =                                 PreferenceManager.getDefaultSharedPreferences(InstrumentationRegistry.getContext())
  1. Save a sample string to the SharedPreferences instance:
@Test
fun testUserParcelization() {

val prefs = getDefaultSharedPreferences()
val userName: String = "Gonzo"
prefs.edit {
putString("user_name", userName)

}
}
  1. 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.
Reset
18.188.26.158