首页 > 解决方案 > Espresso ViewAssertions - Check If Not Null

问题描述

I am runnning Espresso testing and I want to test 2 items:

  1. Click of a button in Fragment X
  2. The TextView contains text in Activity Y

code:

@RunWith(AndroidJUnit4.class)
public class AsyncTest {

    private static final String TEST_TEXT = "TEST_TEXT";

    @Rule
    public ActivityTestRule<MainActivity> mActivity = new ActivityTestRule(MainActivity.class);

    @Test
    public void testIfResultIsEmpty() {

        Espresso.onView(ViewMatchers.withId(R.id.first_button))
          .perform(ViewActions.click());

        /* How do I check that text has populated? */
        Espresso.onView(ViewMatchers.withId(R.id.random_joke))
         .check();
 }

标签: javaandroidandroid-espresso

解决方案


matches(isDisplayed()) would at least check if the view is being displayed - while one barely can check for the text of a random joke, where one would assume some random text... unless loading a specific text for testing purposes, which is then being known - and could be compared/matched (as explained recently in the RecyclerView answer, the ActivityTestRule could be used to pass an ITEM_ID, so that one would have a specific item to compare the views to).

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

and one does not have to use Espresso, but can try findViewById() and then assertNotNull().


推荐阅读