1

Ich habe ein Problem mit dem Testen meines Codes von Expresso. Ich schrieb diesen Code:Android Test Präferenzen Fragment von Expresso

public class SettingsActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getFragmentManager().beginTransaction() 
      .replace(android.R.id.content, new SettingsFragment()) 
      .commit(); 
} 


public class SettingsFragment extends PreferenceFragment { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.preferences); 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/preferences_xml"> 
<PreferenceCategory 
    android:title="@string/application_settings" 
    android:id="@+id/application_settings_preferences"> 

<CheckBoxPreference 
    android:key="@string/key_processing_private_data_preference" 
    android:title="@string/title_processing_private_data_preference" 
    android:summary="@string/summary_processing_private_data_preference" 
    android:defaultValue="true" 
    android:dependency="@string/key_recording_audio_preference" 
    android:id="@+id/private_data_check_box_preference"/> 
</PreferenceCategory> 

<PreferenceCategory 
    android:title="@string/device_settings" 
    android:id="@+id/device_settings_preferences"> 

<CheckBoxPreference 
    android:key="@string/key_recording_audio_preference" 
    android:title="@string/title_recording_audio_preference" 
    android:summary="@string/summary_recording_audio_preference" 
    android:defaultValue="true" 
    android:id="@+id/recording_audio_check_box_preference"/> 

    </PreferenceCategory> 
</PreferenceScreen> 

Meine Testregel:

@Rule 
public ActivityTestRule<SettingsActivity> mActivityRule = new ActivityTestRule<>(
     SettingsActivity.class); 

Ich versuche Test erste Checkbox:

onData(anything()) 
      .inAdapterView(allOf(
        isDescendantOfA(withId(R.id.preferences_xml)), 
        withId(R.id.application_settings_preferences))) 
      .atPosition(1) 
      .check(matches(isDisplayed())); 

-Test immer scheitern mit NoMatchingViewException : Keine Ansichten in der Hierarchie foun d Matching:

http://prntscr.com/bv8xlb

Und auch ich versuche:

String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference); 
    onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 

-Test immer scheitern mit Nullpointer:

http://prntscr.com/bv8w04.

onView(withId(R.id.working_in_background_check_box_preference)).check(matches(isDisplayed())); 

Fehler auch mit NoMatchingViewException.

Würde jemand ein Beispiel mit korrektem Testfall zeigen?

Antwort

3

(Veröffentlicht im Namen des OP).

Gelöst:

onData(allOf(
      is(instanceOf(Preference.class)), 
      withKey(key), 
      withSummary(R.string.summary_working_in_background_preference), 
      withTitle(R.string.title_working_in_background_preference))) 
      .onChildView(withText(title)) 
      .check(matches(isCompletelyDisplayed()));