2016-04-27 66 views
1

我正在使用espresso和AndroidJunit4轉輪。我正在嘗試設置一些首選項,以便在onCreate或onResume觸發該邏輯時進行操作。看起來,在執行onView()之前調用prefs不起作用。我打印出onResume和onCreate中的首選項,它們是空的。注意shouldOpenVerifyFragement()測試用例。運行測試前設置首選項狀態

任何想法或建議如何做到這一點?

@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(); 

} 

}

回答

1

避免自動啓動活動

sut = new ActivityTestRule<>(DriverActivity.class, false, false) 

的當所有設置完成後開始活動

public void shouldOpenVerifyFragement() { 
    ... 
    editor.commit(); 
    sut.launchActivity(null) 
    ... 
} 
相關問題