title: Android testing shared preferences
body: <p>I want to test whether a value is saved correctly to the <code>SharedPreferences</code>.</p>
  
  <p>In my code I have the <code>SharedPreferences</code> injected so I save a value like so:</p>
  
  <pre><code>with(sharedPreferences.edit()) {
      putString("testkey", "dummyValue")
      apply()
  }
  </code></pre>
  
  <p>In my test:</p>
  
  <pre><code>val mockSharedPreferences = mock(SharedPreferences::class.java)
  val mockEditor = mock(SharedPreferences.Editor::class.java)
  
  `when`(sharedPreferences.edit()).thenReturn(mockEditor)
  
  assert(mockSharedPreferences.getString("testkey") == "dummyValue")
  </code></pre>
  
  <p>The problem is that the mockSharedPreferences returns null so what else do I need to mock/change?</p>