I am using espresso and AndroidJunit4 runner. I am trying to set some preferences so that when onCreate or onResume fires that logic will be engaged. It seems that the call to prefs before doing the onView() has no effect. I am printing out the preferences in the onResume and onCreate and they are empty. Notice the shouldOpenVerifyFragement() test case.
Any thought or suggestions on howto do this?
@RunWith(AndroidJUnit4.class)
@LargeTest
public class DriverActivityTest {
@Rule
public final ActivityTestRule<DriverActivity> sut = new ActivityTestRule<>(DriverActivity.class);
@Test
public void shouldOpenVerifyFragement() {
Log.d("TEST", "shouldOpenVerifyFragment");
SharedPreferences settings = sut.getActivity().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("status", Boolean.valueOf(false));
editor.putString("token", "token");
editor.commit();
onView(withText("Pin Verify")).check(matches(isDisplayed()));
}
/**
* Clears everything in the SharedPreferences
*/
@Before
@After
public void clearSharedPrefs() {
SharedPreferences settings = sut.getActivity().getSharedPreferences(APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
}
}