在我的应用程序中,我使用 Retrofit 从服务器获取数据,并使用 Room 将其保存到数据库中。
我想在RecyclerView
adapter
上单击按钮时,将此项目保存到数据库中。
我可以,当单击按钮时,我可以将其保存到 Room 数据库中。
但是我要为此编写测试,但是我不知道从服务器加载数据后如何运行测试!
我编写了将静态数据保存到数据库的测试:
@LargeTest
@RunWith(AndroidJUnit4.class)
public class AddPersonTest {
@Rule
public ActivityTestRule<ListActivity> mActivityTestRule = new ActivityTestRule<>(ListActivity.class);
@Test
public void addPersonTest() {
ViewInteraction floatingActionButton = onView(allOf(withId(R.id.fab), isDisplayed()));
floatingActionButton.perform(click());
ViewInteraction appCompatEditText = onView(
withId(R.id.nameEditText));
appCompatEditText.perform(scrollTo(), replaceText("Daniel Alvarez"), closeSoftKeyboard());
ViewInteraction appCompatEditText2 = onView(
withId(R.id.addressEditText));
appCompatEditText2.perform(scrollTo(), replaceText("La Paz, Bolivia"), closeSoftKeyboard());
ViewInteraction appCompatEditText3 = onView(
withId(R.id.phoneEditText));
appCompatEditText3.perform(scrollTo(), replaceText("591 77242424"), closeSoftKeyboard());
ViewInteraction appCompatEditText4 = onView(
withId(R.id.emailEditText));
appCompatEditText4.perform(scrollTo(), replaceText("daniel@alvarez.tech"), closeSoftKeyboard());
ViewInteraction appCompatEditText5 = onView(
withId(R.id.birthdayEditText));
appCompatEditText5.perform(scrollTo(), click());
ViewInteraction appCompatImageButton = onView(
allOf(withClassName(is("android.support.v7.widget.AppCompatImageButton")), withContentDescription("Previous month"),
withParent(allOf(withClassName(is("android.widget.DayPickerView")),
withParent(withClassName(is("com.android.internal.widget.DialogViewAnimator"))))),
isDisplayed()));
appCompatImageButton.perform(click());
ViewInteraction appCompatImageButton2 = onView(
allOf(withClassName(is("android.support.v7.widget.AppCompatImageButton")), withContentDescription("Previous month"),
withParent(allOf(withClassName(is("android.widget.DayPickerView")),
withParent(withClassName(is("com.android.internal.widget.DialogViewAnimator"))))),
isDisplayed()));
appCompatImageButton2.perform(click());
ViewInteraction appCompatImageButton3 = onView(
allOf(withClassName(is("android.support.v7.widget.AppCompatImageButton")), withContentDescription("Previous month"),
withParent(allOf(withClassName(is("android.widget.DayPickerView")),
withParent(withClassName(is("com.android.internal.widget.DialogViewAnimator"))))),
isDisplayed()));
appCompatImageButton3.perform(click());
ViewInteraction appCompatImageButton4 = onView(
allOf(withClassName(is("android.support.v7.widget.AppCompatImageButton")), withContentDescription("Previous month"),
withParent(allOf(withClassName(is("android.widget.DayPickerView")),
withParent(withClassName(is("com.android.internal.widget.DialogViewAnimator"))))),
isDisplayed()));
appCompatImageButton4.perform(click());
ViewInteraction appCompatButton = onView(
allOf(withId(android.R.id.button1), withText("OK")));
appCompatButton.perform(scrollTo(), click());
ViewInteraction floatingActionButton2 = onView(allOf(withId(R.id.fab), isDisplayed()));
floatingActionButton2.perform(click());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
onView(withId(R.id.nameEditText)).check(matches(withText("Daniel Alvarez")));
onView(withId(R.id.addressEditText)).check(matches(withText("La Paz, Bolivia")));
onView(withId(R.id.phoneEditText)).check(matches(withText("591 77242424")));
onView(withId(R.id.emailEditText)).check(matches(withText("daniel@alvarez.tech")));
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
运行此测试时,将数据添加到数据库中,但是我想从服务器加载数据后,单击适配器的按钮,并在保存到数据库时从适配器的模型中获取数据!
如何编写此测试?
您可以将这个问题的链接发送给我吗?我在Google上搜索但未找到
注意::请不要给我负面意见,我真的需要此帮助。