title: The test still read from the real SharedPreferences
body: <p>I have made a simple instrumented test to verify that if the data read from the SharedPreferences is displayed properly on the UI.Both data-retrieving and displaying actions are performed in Activity's onResume()method.<br>
  But the problem is,even if I've mocked the preference object and defined the fake return value,<strong>the activity still read data from the real preference</strong>,ignoring <code>when(...).thenReturn(...)</code>statement.Does anyone have any idea?  </p>
  
  <pre class="lang-java prettyprint-override"><code>@RunWith(AndroidJUnit4.class)
  public class EditProfileActivityTest {
  
      @Mock
      private UserPreference userPreference;
      private String FAKE_NAME = "Test";
  
      @Rule
      public ActivityTestRule&lt;EditProfileActivity&gt; activityTestRule = new ActivityTestRule(EditProfileActivity.class,true,false);
  
      @Before
      public void setUp(){
  
          //Set fake SharedPreferences
          MockitoAnnotations.initMocks(this);
          when(userPreference.getName()).thenReturn(FAKE_NAME);
  
          //Start Activity
          Intent intent = new Intent();
          activityTestRule.launchActivity(intent);
      }
  
      @Test
      public void showUserData() throws Exception{
          onView(withId(R.id.name_tv)).check(matches(withText(FAKE_NAME)));
      }
  }    
  </code></pre>
  
  <p>where UserPreference is a custom class which simply wraps SharedPreference class and contains lots of getters and setters.This is its constructor  </p>
  
   <pre class="lang-java prettyprint-override"><code>public UserPreference(Context context) {
      this.context = context;
      sharedPreferences = this.context.getSharedPreferences("Pref", Context.MODE_PRIVATE);
      prefEditor = sharedPreferences.edit();
  }  
  </code></pre>
  
  <p>and one of its getter and setter:  </p>
  
   <pre class="lang-java prettyprint-override"><code>public String getName() {
      return sharedPreferences.getString(context.getString(R.string.pref_name), "Guest");
  }    
  public void saveName(String name){
      prefEditor.putString(context.getString(R.string.pref_name), name);
      prefEditor.apply();
  }
  </code></pre>
  
  <p><strong>[EDIT]</strong><br>
  Simplified version of my original Activity:  </p>
  
   <pre class="lang-java prettyprint-override"><code>public class EditProfileActivity extends AppCompatActivity{
      //...
      private UserPreference userPreference;
      //...
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          userPreference = new UserPreference(this);
          setContentView(R.layout.activity_edit_profile);
          //...
      }
      @Override
      protected void onResume() {
          super.onResume();
          //...
          String name = userPreference.getName();
          nameEdt.setText(name);  //Display the name on an EditText
          //...
      }
  }
  </code></pre>