Expresso的Android测试偏好片段

Expresso的Android测试偏好片段,android,testing,android-preferences,checkboxpreference,expresso,Android,Testing,Android Preferences,Checkboxpreference,Expresso,我在用Expresso测试代码时遇到了问题。我写了这段代码: public class SettingsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction() .replace(a

我在用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:

测试始终失败,NoMatchingViewException:在层次结构中找不到匹配的视图:

我还尝试:

String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference);
   onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 
测试始终失败,出现NullPointerException:

也会因NOMATCHINGVIEWEException而失败

有人会用正确的测试用例展示一个例子吗?

代表OP发布

已解决:

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()));
onData(anything())
            .inAdapterView(allOf(
                    isDescendantOfA(withId(R.id.preferences_xml)),
                    withId(R.id.application_settings_preferences)))
            .atPosition(1)
            .check(matches(isDisplayed()));
String workingInBackgroundSummary = mActivityRule.getActivity().getBaseContext().getString(R.string.summary_working_in_background_preference);
   onData(withSummaryText(workingInBackgroundSummary)).check(matches(isDisplayed())); 
onView(withId(R.id.working_in_background_check_box_preference)).check(matches(isDisplayed()));
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()));