Espresso - TextView contains String

Quite simply, how do I say if a given TextView contains a specific string in Espresso.

The equivalent of: myStrings.contains("Subby");


Solution 1:

You can use the Hamcrest library. It has a method containsString. I believe it is in the Espresso library.

You can static import it in your class:

import static org.hamcrest.core.StringContains.containsString;

Use containsString in your method on a TextView:

textView.check(matches(withText(containsString("Test"))));

Solution 2:

Use withText

onView(...).check(matches(withText("Subby")));

onView(withId(R.id.textView)).check(matches(withText("Subby")));

Solution 3:

Use withSubstring(substring), it is same as withText(containsString(substring)) but more concise