title: Test SharedPreferences in Android
body: <p>I am coming into a legacy codebase which uses SharedPreferences for data persistence.  I'd like to test saving/retrieving a value, mocking using <strong>MockK</strong>.  However, the assertion in this unit test never passes.  It is as if the SharedPrefs aren't stored properly in testing: </p>
  
  <pre><code>class MyProfilePrefsTest {
    private lateinit var myProfilePrefs: ProfilePrefs
    @RelaxedMockK private lateinit var mockSharedPrefs: SharedPreferences
    @RelaxedMockK private lateinit var context: Context
  
    @Before
    fun setup() {
      MockKAnnotations.init(this)
  
      val sharedPreferences = mockk&lt;SharedPreferences&gt;()
      every { sharedPreferences.edit() } returns (mockk())
      myProfilePrefs = ProfilePrefs(context, sharedPreferences)
  
      mockStatic(DeviceInfo::class)
      every { DeviceInfo.serialNumber() } returns "fake_serial"
  }
  
  @Test
  fun `Saving correct cellular download pref for device id`() {
      // Arrange
      val isEnabled = true
  
      // Act
      myProfilePrefs.setCellularDownloadingEnabled(isEnabled)
  
      // Assert
      assertTrue(myProfilePrefs.getCellularDownloadingEnabled())
  }}
  </code></pre>
  
  <p>Anybody know how to unit test SharedPrefs?</p>